Reputation: 1418
I have an If staement that is not behaving as I would have intended.
Here is my example:
if not userTime[-2].upper() == "X" or not userTime[-2].upper() == "Z":
raise ValueError("not entered an X or a Z")
else:
notValid = False
My input would always result in userTime[-2]
always being a capital 'Z'
Printing out the userTime[-2].upper()
to the screen appears as a 'Z' Yet it still raises the exception.
I cannot get it to hit the else
part of this 'if' statement and am now wondering if there is something I have missed out
Upvotes: 1
Views: 71
Reputation: 76254
The opposite of a or b
isn't not a or not b
, it's not a and not b
. Try:
if not userTime[-2].upper() == "X" and not userTime[-2].upper() == "Z":
Or, equivalently,
if userTime[-2].upper() != "X" and userTime[-2].upper() != "Z":
Or, avoiding the issue of complex boolean expressions entirely,
if userTime[-2].upper() not in ("X", "Z"):
Negating boolean expressions is a bit counterintuitive. Consult De Morgan's laws for more information.
Upvotes: 6