如何使用if else语句避免检查所有可能性?

时间:2018-03-20 00:32:29

标签: python-3.x if-statement simplify

我刚刚开始学习Python(昨天),我想知道如何更有效地制作我的第一个程序之一。

基本上,我的问题是我如何制作一个需要直接输入的程序,但是没有使用if else语句检查每种可能性。我的程序非常简单,只需要设置用户名和密码并重新输入,但我必须手动设置所有可能性。例如,如果您输入用户名并输入错误,则可以重置该用户名。谢谢您的帮助!

这是我的代码:

def main():
tries = 2
username = raw_input("Please enter a Username ")
confirmation = raw_input("Is your Username: " + username + "? ")
if(confirmation == "yes") or (confirmation == "Yes"):
    password = raw_input("Please enter a Password for " + username + " ")
    passconfirm = raw_input("Is your Password " + password + "? ")
    if(passconfirm == "yes") or (passconfirm == "Yes"):
        #username and password
        enteruser = raw_input("Please enter your Username: ")
        if(enteruser == username):
            enterpass = raw_input("Please enter your Password for " + username + " ")
            if(enterpass == password):
                print("Welcome " + username)
            else:
                while(tries > 0):
                        enterpass = raw_input("Incorrect Password, Please re-enter your Password for " + username + " ")
                        if (enterpass == password):
                            print("Welcome " + username)
                            exit()
                        else:
                            tries = tries - 1
                print("Password incorrect")
        else:
            while(enteruser != username):
                enteruser = raw_input("Username not recognized, please retry ")
            enterpass = raw_input("Please enter your Password for " + username + " ")
            if (enterpass == password):
                print("Welcome " + username)
            else:
                while (tries > 0):
                    enterpass = raw_input("Incorrect Password, Please re-enter your Password for " + username + " ")
                    if (enterpass == password):
                        print("Welcome " + username)
                        exit()
                    else:
                        tries = tries - 1
                print("Password incorrect")
    else:
        while(passconfirm != "yes") and (passconfirm != "Yes"):
            #username and password2
            password2 = raw_input("Please re-enter a Password for " + username + " ")
            passconfirm = raw_input("Is your Password " + password2 + "? ")
        enteruser = raw_input("Please enter your Username: ")
        if (enteruser == username):
            enterpass = raw_input("Please enter your Password for " + username + " ")
            if (enterpass == password2):
                print("Welcome " + username)
            else:
                while (tries > 0):
                    enterpass = raw_input("Incorrect Password, Please re-enter your Password for " + username + " ")
                    if (enterpass == password2):
                        print("Welcome " + username)
                        exit()
                    else:
                        tries = tries - 1
                print("Password incorrect")
        else:
            while (enteruser != username):
                enteruser = raw_input("Username not recognized, please retry ")
            enterpass = raw_input("Please enter your Password for " + username + " ")
            if (enterpass == password2):
                print("Welcome " + username)
            else:
                while (tries > 0):
                    enterpass = raw_input("Incorrect Password, Please re-enter your Password for " + username + " ")
                    if (enterpass == password2):
                        print("Welcome " + username)
                        exit()
                    else:
                        tries = tries - 1
                print("Password incorrect")
else:
    while(confirmation != "yes") and (confirmation != "Yes"):
        username2 = raw_input("Please re-enter your Username ")
        confirmation = raw_input("Is your Username: " + username2 + "? ")
    password = raw_input("Please enter a Password for " + username2 + " ")
    passconfirm = raw_input("Is your Password " + password + "? ")
    if (passconfirm == "yes") or (passconfirm == "Yes"):
        #username2 password
        enteruser = raw_input("Please enter your Username: ")
        if (enteruser == username2):
            enterpass = raw_input("Please enter your Password for " + username2 + " ")
            if (enterpass == password):
                print("Welcome " + username2)
            else:
                while (tries > 0):
                    enterpass = raw_input("Incorrect Password, Please re-enter your Password for " + username2 + " ")
                    if (enterpass == password):
                        print("Welcome " + username2)
                        exit()
                    else:
                        tries = tries - 1
                print("Password incorrect")
        else:
            while (enteruser != username2):
                enteruser = raw_input("Username not recognized, please retry ")
            enterpass = raw_input("Please enter your Password for " + username2 + " ")
            if (enterpass == password):
                print("Welcome " + username2)
            else:
                while (tries > 0):
                    enterpass = raw_input("Incorrect Password, Please re-enter your Password for " + username2 + " ")
                    if (enterpass == password):
                        print("Welcome " + username2)
                        exit()
                    else:
                        tries = tries - 1
                print("Password incorrect")
    else:
        while (passconfirm != "yes") and (passconfirm != "Yes"):
            #username2 password2
            password2 = raw_input("Please re-enter a Password for " + username2 + " ")
            passconfirm = raw_input("Is your Password " + password2 + "? ")
        enteruser = raw_input("Please enter your Username: ")
        if (enteruser == username2):
            enterpass = raw_input("Please enter your Password for " + username2 + " ")
            if (enterpass == password2):
                print("Welcome " + username2)
            else:
                while (tries > 0):
                    enterpass = raw_input("Incorrect Password, Please re-enter your Password for " + username2 + " ")
                    if (enterpass == password2):
                        print("Welcome " + username2)
                        exit()
                    else:
                        tries = tries - 1
                print("Password incorrect")
        else:
            while (enteruser != username2):
                enteruser = raw_input("Username not recognized, please retry ")
            enterpass = raw_input("Please enter your Password for " + username2 + " ")
            if (enterpass == password2):
                print("Welcome " + username2)
            else:
                while (tries > 0):
                    enterpass = raw_input("Incorrect Password, Please re-enter your Password for " + username2 + " ")
                    if (enterpass == password2):
                        print("Welcome " + username2)
                        exit()
                    else:
                        tries = tries - 1
                print("Password incorrect")

main()的

0 个答案:

没有答案