python - 即使条件正确,while 循环也不会结束

标签 python while-loop

我正在做一个登录系统。它要求用户回答问题(“您已经有帐户了吗?”),虽然答案不是"is"或“否”,但它应该继续询问。

print('Welcome to the quiz!')
print('Do you already have an account? ')
have_acc = input()
while have_acc != ('Yes') or have_acc != ('No') or have_acc != ('yes') or have_acc != ('no'):
    print('Please enter Yes or No')
    have_acc = input()

输出

Welcome to the quiz!
Do you already have an account? 
yes
Please enter Yes or No
no
Please enter Yes or No
Yes
Please enter Yes or No
No
Please enter Yes or No

最佳答案

更规范的方法:

while input.lower() not in ('yes', 'no',):
    ... your retry code ...

关于python - 即使条件正确,while 循环也不会结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46755238/

相关文章:

python - 在Python中,DOWNLOAD_DELAY和time.sleep有什么区别?

python - OSError : MoviePy error: the file guitar. mp4 找不到

python - 是否可以在cassandra中创建带有uuid的表?

PHP - while mysql_fetch_array 循环错误?

java - 一旦读取大约 30,000 个邮政编码,程序就会出现滞后

Python:Scrapy start_urls 列表能够处理 .format()?

python - Pandas 数据框通过比较所有其他行创建新列

python - 关于兼容 Python 3.0 的 GUI 编辑器

c - 添加值的递归函数正在输出异常值

java - .hasNext() 和 .next() 导致无限 while 循环