我目前对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()
我很想知道为什么会这样,还有一个简单的修复方法.非常感谢您的帮助!
您的q1
变量是一个整数,但您将其与条件中的字符串进行比较.它永远不会是True
.
>>> 1 == "1" False
用while q1 not in [1, 2, 3, 4]:
.替换你的病情.