完全没有人需要帮助!空翻

2024-10-06 11:41:50 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码:

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!"

当我运行代码时,这是我输入值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的输入,用户自动得到“请选择一个有效的输入!”警告


Tags: torunyouinputyourrawintme