Skip to content Skip to sidebar Skip to footer

Or Statement Handling Two != Clauses Python

(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: p

Solution 1:

You need and:

whileinput != 10andinput != 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.

Solution 2:

....or a different way to express it that may seem more natural to you:

whileinputnotin (10, 20):
    # your code here...

Solution 3:

Did you mean to have the bet be input. And I think you meant to say if input if not 10 and is not 20.

input = int(raw_input())
whileinput != 10andinput != 20:
    print'Incorrect value, try again'input = int(raw_input())

Solution 4:

I think you want an and there.

whileinput != 10orinput != 20:

This will repeat forever - if input is 10, then the first condition is false. if input is 20, the second condition is false. input can never be both 10 and 20, so that's equivalent to true.

Solution 5:

You want "and" and not "or". Think about your boolean logic.

Post a Comment for "Or Statement Handling Two != Clauses Python"