OR statement handling two != clauses Python

thebill picture thebill · Apr 15, 2011 · Viewed 72.7k times · Source

(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())

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!

Answer

Felix Kling picture Felix Kling · Apr 15, 2011

You need and:

while input != 10 and input != 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.