Hans de Jong
Hans de Jong

Reputation: 2078

Python Problems with if statement

I have a problem with this piece of code. Somehow if we put in 'register' or 'Register' as input, it goes to the register function, but after it prints the else: Aswell ("Bad input"); I have made more of these but cant find the fault in this one.

Please help me. Thanks! Here's my code:

def Boot(): 
        print "Type 'Login' + accountname to login."

        x = raw_input("> ")
        Name = x.split()

        if x == "Register" or x == "register":
            print "Registerbla"

        if Name[0] == "Login" or Name[0] == "login":
            print "Loginblabla"
        else:
            print "Bad input"

So what I see after input is: Registerbla Bad input

Upvotes: 0

Views: 1318

Answers (2)

thegrinner
thegrinner

Reputation: 12243

You're missing the else portion of your if statement. Without it, you actually check two separate if statements: the register section and the login/bad input section. Instead, you should use elif:

    if x == "Register" or x == "register":
        print "Registerbla"
    elif Name[0] == "Login" or Name[0] == "login":
        print "Loginblabla"
    else:
        print "Bad input"

Also, consider changing your statements to check against lowercase, like

if x.lower() == "register":
    # Now any capitalized variant of register will work!

Upvotes: 2

dm03514
dm03514

Reputation: 55932

when you type in register or Register

if Name[0] == "Login" or Name[0] == "login":

evaluates to false, printing Bad input

Upvotes: 1

Related Questions