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?

Asked By: Jelle

||

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"):
Answered By: Rahul K P

Use not in instead of != to compare to each individual value

Answered By: Lisa
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.