Skip to content

Instantly share code, notes, and snippets.

@wasi-master
Last active December 31, 2021 09:25
Show Gist options
  • Save wasi-master/206769690238574e8c360b804ea12416 to your computer and use it in GitHub Desktop.
Save wasi-master/206769690238574e8c360b804ea12416 to your computer and use it in GitHub Desktop.

Got code that looks like this, and it's not working properly?

if x == "foo" or "bar" or "baz":

Python is interpreting this as though it was this:

if (x == "foo") or ("bar") or ("baz"):

If the first expression (x == "foo") isn't true, the second ("bar") will be, so this compound conditional always passes. Try this instead:

if x == "foo" or x == "bar" or x == "baz":

or, even better:

if x in ("foo", "bar", "baz"):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment