编码新手,在为程序进行万无一失的输入时遇到问题:
answer=input(x)
while (answer != "yes") or (answer != "no"):
answer=input("must be 'yes' or 'no' answer")
无论输入如何,while 循环都不会停止。我究竟做错了什么?
最佳答案
这是我会用到的:
while answer not in ("yes", "no"):
现在,您的代码一直在运行,因为answer
将始终不是"yes"
或不是"no"
.
此外,如果您愿意,可以添加 .lower()
像这样:
while answer.lower() not in ("yes", "no"):
这将允许您的代码处理诸如 "Yes"
、"yEs"
等输入。
关于python - 在 python 中使用或,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20013961/