'Broken prompt, simple if statements? (Python)

def prompt():
    x = raw_input('Type a command: ')
    return x


def nexus():
    print 'Welcome to the Nexus,', RANK, '. Are you ready to fight?';
    print 'Battle';
    print 'Statistics';
    print 'Shop';
    command = prompt()
    if command == "Statistics" or "Stats" or "Stat":
        Statistics()
    elif command == "Battle" or "Fight":
        Battle()
    elif command == "Shop" or "Buy" or "Trade":
        Shop()
    else:
        print "I can't understand that..."
        rankcheck()

Practically, what that's supposed to do is take you to the Stat function when stat is typed, Battle function when Battle is typed, and shop function when shop is typed. I'm having problems actually getting it to work (Duh), however. When anything at all is typed, it takes me straight to the Stat function. I believe it's because of the way I'm handling the prompt. It pretty much only sees the very first if statement and renders the function like it should. If I type Battle, however, it still takes me to statistics.



Solution 1:[1]

The condition

command == "Statistics" or "Stats" or "Stat"

is always considered True. It either evaluates to True if command is Statistics, or it evaluates to "Stats". You probably want

if command in ["Statistics", "Stats", "Stat"]:
    # ...

instead, or better

command = command.strip().lower()
if command in ["statistics", "stats", "stat"]:

to be a bit more relaxed.

Solution 2:[2]

"Stats" is a non-zero length string, so it acts as the boolean True. Try using in with a sequence instead:

if command in ("Statistics", "Stats", "Stat"):
    Statistics()
elif command in ("Battle", "Fight"):
    Battle()
elif command in ("Shop", "Buy", "Trade"):
    Shop()
else:
    print "I can't understand that..."
    rankcheck()

Solution 3:[3]

Also, when using simple if with the and/or statement, make sure to always reference the element being compared. For example,

if command == "Statistics" or "Stats" or "Stat":
        Statistics()

would be

if command == "Statistics" or command == "Stats" or command == "Stat":
        Statistics()

However, as said before, it would be better to use a simple "in" keyword

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sven Marnach
Solution 2 robert
Solution 3 Estarius