python - 在 python 中使用或

标签 python syntax

编码新手,在为程序进行万无一失的输入时遇到问题:

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/

相关文章:

python - Scrapy仅抓取给定页面

C++语法问题

SQL JOIN : is there a difference between USING, ON 或 WHERE?

python - Django ImportError 没有命名的模块

python - 将 caffe 与 c++ 或 python 连接时出现问题

python - 对带有异常的字符串进行标题化

python - 可以简化或更好地编写此 Python 程序吗?

python - Python 的双面不等式是如何工作的?为什么它不适用于 numpy 数组?

parsing - 用于 Python 样式缩进的 PEG

java - token 错误 ";",{预期 GUI