随机化和改组问题

时间:2013-09-15 06:38:28

标签: python

我是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)
回答完第一个问题后,它出错了。任何解决方案或帮助?谢谢!

1 个答案:

答案 0 :(得分: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)