Python 如何使程序循环,直到用户输入正确的用户名和密码

Python 如何使程序循环,直到用户输入正确的用户名和密码,python,Python,我已经做了一个程序,要求输入用户名和密码,直到输入正确 代码: def main(): endProgram ="no" while endProgram =="no": print("Welcome to the program :D") print("-------------------------") username =778922 password =2713 login = ""

我已经做了一个程序,要求输入用户名和密码,直到输入正确

代码:

def main():
    endProgram ="no"
    while endProgram =="no":
        print("Welcome to the program :D")
        print("-------------------------")

        username =778922
        password =2713
        login = ""
        while login ==username:
            username = int(input("Username: "))
            password = int(input("Passowrd: "))


        endProgram = raw_input("Do you wish to end the program")

main()

您的
while
循环将不会有任何不同,因为您将
endProgram
设置为
'no'

以下代码将提示用户输入其登录信息(用户名),如果找不到用户名,请重复此代码:

def main():
    endProgram = input('do you wish to end the program?')
    while endProgram == 'no':
        print('Welcome')
        username = 778922
        password = 2713
        login = input('What\'s your login info?')
        if login==username:
            username = int(input("Username: "))
            password = int(input("Password: "))

main()

将while循环的条件更改为:

while username != input_name or password != input_password:
    #continue to take inputs or end program
这样,内部while循环将继续循环,直到输入正确的用户名。要退出此循环,如果用户无法输入正确的用户名或密码,可以执行以下操作

endProgram = raw_input("Do you wish to end the program")
if endProgram == "yes":
    break

他正在使用Python3,我建议您将
raw\u input
更改为
input