Why Doesn't This "is Not" If Statement Work?
This is my code, if diff != '1' or diff != '2' or diff != '3': print('You need to pick either 1, 2 or 3\n') For some reason, the outcome is, Pick a difficulty: 1) Ea
Solution 1:
To apply the logic of "not any" you would want to check if it is any of the valid results then invert. (NOR)
if not (diff == "1" or diff == "2" or diff == "3"):
Or applying DeMorgan's theorem this would be equivelent to "not equal to 1 AND not equal to 2 AND not equal to 3"
if diff != "1" and diff != "2" and diff != "3":
of course python also has the in
and not in
operator which makes this much cleaner:
if diff not in ("1", "2", "3"):
Solution 2:
You need to use and instead of or. If you input 1, then diff != "1"
returns True.
Your code should look like this:
if diff != "1" and diff != "2" and diff != "3":
print("You need to pick either 1, 2 or 3\n")
Post a Comment for "Why Doesn't This "is Not" If Statement Work?"