简体   繁体   English

Python 嵌套列表:如何将特定元素和 append 打印到每个子列表

[英]Python Nested List: How to print specific elements and append to each sublist

quizes = [["Andrew"], ["Amy"], ["Jared"], ["Bob"], ["Sarah"]]

for i in range(len(quizes)):
    grade = eval(input("Enter", quizes[i][0],"grade:"))
    quizes[i].append(grade)
    print(quizes[i])

Hey guys so I been working on this for the past week and can't solve this.嘿伙计们,所以我过去一周一直在研究这个问题,但无法解决这个问题。 I am trying to get my program to ask "Enter [person1] grade: " and so on and append a second element to each sublist which will be their grade and then print the contents of the entire list.我试图让我的程序询问“输入 [person1] 等级:”等等,append 是每个子列表的第二个元素,这将是他们的等级,然后打印整个列表的内容。 Any help is appreciated任何帮助表示赞赏

You need to change:你需要改变:

    grade = eval(input("Enter", quizes[i][0],"grade:"))

to

    grade = eval(input("Enter " + quizes[i][0] + " grade:"))

input does not behave as print does. input的行为不像print那样。 You can't use commas to join text in any other function (except for a very small number of custom functions and modules), and you should stay away from them at if that's confusing for you to only use them sometimes.您不能使用逗号在任何其他 function 中加入文本(极少数自定义函数和模块除外),如果您有时只使用它们会感到困惑,您应该远离它们。

With that change, does your code work now?有了这个改变,你的代码现在可以工作了吗? You didn't tell us why it wasn't working the way you expected.您没有告诉我们为什么它没有按您预期的方式工作。

The main problem is that input() accepts only one argument as the input prompt, unlike print which can take any number of arguments - you're giving 3 parameters to the input function - "Enter" , quizes[i][0] , and "grade:" .主要问题是input()只接受一个参数作为输入提示,不像print可以接受任意数量的 arguments - 你给输入 function - "Enter" , quizes[i][0] 3个参数,和"grade:"

You can try concatenating them - like input("Enter "+str(quizes[i][0])+" grade:") (the str() conversion is not needed if you know that all the quizes[i][0] will be strings already), or even use string formatting - "Enter {} grade".format(quizes[i][0]) or f"Enter {quizes[i][0]} grade:"您可以尝试将它们连接起来 - 例如 input("Enter "+str str() input("Enter "+str(quizes[i][0])+" grade:") (如果您知道所有的quizes[i][0 ]已经是字符串),甚至使用字符串格式 - "Enter {} grade".format(quizes[i][0])f"Enter {quizes[i][0]} grade:"

This should be enough, but there are 2 more changes you can make to your code if you like -这应该足够了,但是如果您愿意,您还可以对代码进行 2 处更改 -

  • Iterate over the nested lists directly ( for sub_list in quizes: )直接遍历嵌套列表( for sub_list in quizes:
  • Using int() or float() to convert a returned input string containing a number will also work in place of eval使用int()float()转换包含数字的返回输入字符串也可以代替eval

For example例如

for quiz in quizes:
   grade = eval(input("Enter {} grade:".format(quiz[0])))
   quiz.append(grade)
   print(quiz)

EDIT: Python docs on string formatting The f"..." syntax works only for Python 3.6+编辑: Python 文档关于字符串格式f"..."语法仅适用于 Python 3.6+

The problem is input takes 1 string as a parameter, you are passing 3 instead.问题是输入将 1 个字符串作为参数,而您传递的是 3。 So:所以:

quizes = [["Andrew"], ["Amy"], ["Jared"], ["Bob"], ["Sarah"]]

for l in quizes:
    grade = eval(input(f"Enter {l[0]} grade:"))
    l.append(grade)
    print(l)

However, I respectfully don't understand the point of using eval here.但是,我不明白在这里使用eval的意义。 Eval turns data into code. eval将数据转换为代码。 Using eval on user input creates a great security hole.对用户输入使用 eval 会造成很大的安全漏洞。 In the above code, what if the user input quizes.clear() ?在上面的代码中,如果用户输入quizes.clear()怎么办? The whole array will be emptied.整个数组将被清空。 What if he inputs something eviler?如果他输入更邪恶的东西怎么办?

Consider (assumes valid grades only contain numbers):考虑(假设有效成绩仅包含数字):

quizes = [["Andrew"], ["Amy"], ["Jared"], ["Bob"], ["Sarah"]]

for l in quizes:
    while True:
        try:
            grade = float(input(f"Enter {l[0]} grade:"))
        except ValueError:
            print("Please input a number.")
        else:
            l.append(grade)
            break
print(quizes)

Now that I'm looking at it, what did yours do wrong?现在我在看它,你做错了什么?

quizes = [["Andrew"], ["Amy"], ["Jared"], ["Bob"], ["Sarah"]]

for quiz in quizes:
    grade = eval(input("Enter " + quiz[0] + " grade:"))
    quiz.append(grade)
    print(quiz)

print(quizes)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM