这是我的代码:
police = "Stop your vehicle!"
print police
thief = int(raw_input("0 to stop, 1 to run: "))
while thief != 0 or 1:
thief = int(raw_input("0 to stop, 1 to run: "))
print thief
if thief == 0:
print "A'ight! You got me!"
break
elif thief == 1:
print "You'll never catch me, copper!"
break
else:
print "Please choose a valid input!"
当我运行代码时,这是raw_inputting值0时得到的输出:
Stop your vehicle!
0 to stop, 1 to run: 0
0 to stop, 1 to run: 0
0
A'ight! You got me!
然后程序就像我想要的那样打破了。当我输入时,同样的事情发生1.即使我第一次提示我输入0或1,为什么它会提示输入两次?此外,在打印句子之前,你得到了我",为什么打印出零?当我输入其他类似4的东西时,会发生这种情况:
Stop your vehicle!
0 to stop, 1 to run: 4
0 to stop, 1 to run: 4
4
Please choose a valid input!
0 to stop, 1 to run: 4
这很好,因为循环继续进行,正如我所希望的那样,但我们再次遇到与上述相同的问题。
另外,如果我输入一个字符串,自然会出现语法错误。我怎样才能改进代码,以便在输入任何非0或1的东西时,用户自动获得"请选择有效的输入!"警告?