使用或在python中

use*_*454 2 python syntax

编码新手,为程序输入万无一失的问题:

answer=input(x)
while (answer != "yes") or (answer != "no"):
    answer=input("must be 'yes' or 'no' answer")
Run Code Online (Sandbox Code Playgroud)

无论输入如何,while循环都不会停止.我究竟做错了什么?

iCo*_*dez 8

这是我会用的:

while answer not in ("yes", "no"):
Run Code Online (Sandbox Code Playgroud)

现在,你的代码运行持续,因为answer永远"yes"与否"no".


此外,如果您愿意,您可以这样添加.lower():

while answer.lower() not in ("yes", "no"):
Run Code Online (Sandbox Code Playgroud)

这将使你的代码来处理输入,如"Yes","yEs"


Bre*_*arn 7

一切都不是不是.你想要的and.