0

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

4

2 回答 2

2

您缺少elseif 语句的部分。没有它,您实际上会检查两个单独的 if 语句:注册部分和登录/错误输入部分。相反,您应该使用elif

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

另外,考虑更改您的语句以检查小写,例如

if x.lower() == "register":
    # Now any capitalized variant of register will work!
于 2013-02-20T13:59:31.443 回答
1

当您输入registerRegister

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

评估为假,打印Bad input

于 2013-02-20T13:59:01.947 回答