0

我是 python 的新手,现在我在使用这个程序时遇到了问题

from random import shuffle
question = [["What's the color of the sky when night?", "black"],
            ["How many numbers in this given: ak2ks1l2", "3"],
            ["Who's the man's best friend?", "dog"]]
shuffle(question)

for i in range(3):
    answer = question[i][1]
    question = question[i][0]
    given_answer = input(question)
    if answer == given_answer:
        print("Correct")    
    else:
        print("Incorrect, correct was:", answer)

回答第一个问题后出错。任何解决方案或帮助?谢谢!

4

1 回答 1

2

您正在循环中覆盖question变量:for

question = question[i][0]

使用另一个变量名。

from random import shuffle
questions = [["What's the color of the sky when night?", "black"],
            ["How many numbers in this given: ak2ks1l2", "3"],
            ["Who's the man's best friend?", "dog"]]
shuffle(questions)

for question, answer in questions: # easier to read than: for i in range(3):..[i]
    given_answer = input(question)
    if answer == given_answer:
        print("Correct")    
    else:
        print("Incorrect, correct was:", answer)
于 2013-09-15T06:39:51.097 回答