Hey guys again, but i got another question. With this code here:

if choice == 'help' or '/help':
    print "Test"

what if you entered anything else besides '/help' or 'help' and you have defined other things in the code you want to go to from input? any ideas/suggestions, Thanks

8 Years
Discussion Span
Last Post by jrcagle

That code won't work. Here's why:

"or" doesn't mean "one of these" in Python, or any other computing language. Instead, "or" means "True if one or both operands is True; False if both are False."

So apply that rule to what you wrote:

"choice == help"



Since the second one, "/help", is a non-empty string, it is True (according to Python).

Therefore, no matter what value the choice variable has, your conditional will always be True, and "Test" will always be printed.

Instead, what you want is this:

if choice == 'help' or choice == '/help':
    print "Test"

Now if we apply the same rule (True if one or both is True), we have

choice == 'help'


choice == '/help'

which will be true if choice has either of those two values.

if you have a lot of possibilities, here's a shorter way:

if choice in ["help", "/help"]:
   print "Test"

Hope that /helps.


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.