OR语句处理两个!=子句Python [英] OR statement handling two != clauses Python
问题描述
(使用Python 2.7)我理解这是很基本的,但是为什么下面的语句不能按原样工作:
(Using Python 2.7) I understand this is pretty elementary but why wouldn't the following statement work as written:
input = int(raw_input())
while input != 10 or input != 20:
print 'Incorrect value, try again'
bet = int(raw_input())
基本上,我只想接受10或20作为答案.现在,无论输入",即使是10或20,我都会得到错误值".这些条款会自相矛盾吗?我认为只要其中一个子句是正确的,OR语句就会说确定".谢谢!
Basically I only want to accept 10 or 20 as an answer. Now, regardless of 'input', even 10, or 20, I get 'Incorrect value'. Are these clauses self conflicting? I thought that the OR statement would say OK as long as one of the clauses was correct. Thanks!
推荐答案
您需要 and
:
You need and
:
while input != 10 and input != 20:
仔细考虑:如果input
是10
,则第一个表达式是false
,从而使Python评估第二个表达式input != 20
. 10
是不同于20
的形式,因此该表达式的值为true
.作为false or true == true
,整个表达式为true
.
与20
相同.
Think it through: If the input
is 10
, then the first expression is false
, causing Python to evaluate the second expression input != 20
. 10
is different form 20
, so this expressions evaluates to true
. As false or true == true
, the whole expression is true
.
Same for 20
.
这篇关于OR语句处理两个!=子句Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!