当前位置:  开发笔记 > 编程语言 > 正文

循环无限循环(2.7)

如何解决《循环无限循环(2.7)》经验,为你挑选了1个好方法。

我目前对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()

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



1> Cairnarvon..:

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

>>> 1 == "1"
False

while q1 not in [1, 2, 3, 4]:.替换你的病情.

推荐阅读
和谐啄木鸟
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有