Python NOT equal operator combining with OR operator
Question:
piece of example code:
while input("Type STOP to stop this program: ") != ('STOP' or "stop"):
continue
The program doesn’t give the correct output when inputted: stop.
Why does STOP work but lower case doesn’t?
Answers:
You can convert the input
value to lower case,
while input("Type STOP to stop this program: ").lower() != "stop":
Or use not in
,
while input("Type STOP to stop this program: ") not in ("stop", "STOP"):
Use not in
instead of !=
to compare to each individual value
piece of example code:
while input("Type STOP to stop this program: ") != ('STOP' or "stop"):
continue
The program doesn’t give the correct output when inputted: stop.
Why does STOP work but lower case doesn’t?
You can convert the input
value to lower case,
while input("Type STOP to stop this program: ").lower() != "stop":
Or use not in
,
while input("Type STOP to stop this program: ") not in ("stop", "STOP"):
Use not in
instead of !=
to compare to each individual value