0

我目前对 Python 还很陌生,只是提个醒。

我一直在做一个多项选择测验,似乎我所做的每一次改进,都会出现更多的问题。这是测验本身的一个片段。问题是,每当我运行此代码时,while 循环都会继续重复,并且不会进入下一部分 q2。

def q1(name):
    print
    q1= int(raw_input(
        """Question #1
Which of the following animals is native to New Zealand?
1) Possum
2) Rat
3) Tuatara
4) Sheep
Please enter your answer (1, 2, 3, 4)"""))
    while q1 not in ["1", "2", "3", "4"]:
        q1 = int(raw_input("choose 1, 2, 3 or 4."))

        if q1 == 1 :
            print
            print"That is an incorrect answer. The possum is a native species of Australia."
            W_A()
        elif q1 == 2 :
            print
            print"That is an incorrect answer. The rat arrived on ships, assumedly along with first human inhabitants of New Zealand."
            W_A()
        elif q1 == 3 :
            print
            print'Well done',name.title(),'. That is the correct answer. The tuatara is endemic to New Zealand, and due to its close relation to the dinosaurs that roamed millions of years ago, they are sometimes referred to as a "Living fossil".'
            R_A()
        elif q1 == 4 :
            print
            print 'That is an incorrect answer. Sheep arrived with immigrants from Europe in the 1800s for agricultural purposes. They were used to provide wool and meat for farmers.'
            W_A()

我很想知道为什么会这样,以及一个简单的修复方法。非常感谢您的帮助!

4

1 回答 1

6

您的q1变量是一个整数,但您将它与条件中的字符串进行比较。它永远不会是True

>>> 1 == "1"
False

将您的条件替换为while q1 not in [1, 2, 3, 4]:.

于 2013-05-24T10:18:44.547 回答