In an attempt to make my if statements cleaner, I figured out a simply way of making them quite a lot shorter. Instead of doing this:

elif talk_in == "quit" or talk_in == "q" or talk_in == "exit" or talk_in == "x":

I can do this:

elif talk_in == ("quit" or "q" or "exit" or "x"):

However, it will only return true if the first item within the parentheses makes it true. For example:

>>> talkin = "quit"
>>> talkin == ("quit" or "q")
True
>>> talkin == ("q" or "quit")
False

Why does this happen? And how can I fix it?

Recommended Answers

All 2 Replies

Member Avatar for sravan953

I have no idea why it doesn't work, but this works:

talking="quit"
talking=="q" or "quit"
#True

The correct way to do this is

elif talk_in in ("quit", "q", "exit", "x"):
    etc
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.