gpt4 book ai didi

python - OR 语句处理两个 != 子句 Python

转载 作者:太空宇宙 更新时间:2023-11-03 12:12:26 25 4
gpt4 key购买 nike

(使用 Python 2.7)我知道这是非常基础的,但为什么下面的语句不能像写的那样工作:

input = int(raw_input())
while input != 10 or input != 20:
print 'Incorrect value, try again'
bet = int(raw_input())

基本上我只想接受 10 或 20 作为答案。现在,无论“输入”如何,即使是 10 或 20,我都会得到“不正确的值”。这些条款自相矛盾吗?我认为只要其中一个子句正确,OR 语句就会说 OK。谢谢!

最佳答案

您需要:

while input != 10 and input != 20:

想一想:如果输入10,那么第一个表达式是false,导致Python计算第二个表达式 >输入!= 201020 不同,因此该表达式的计算结果为 true。作为false or true == true,整个表达式为true
20 也是如此。

关于python - OR 语句处理两个 != 子句 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5681271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com