Start New Discussion within our Software Development Community

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

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"

or

"/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'

or

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.

Jeff

This question has already been answered. Start a new discussion instead.