Python - 如何摆脱while循环?

时间:2016-05-22 19:28:12

标签: python loops while-loop

我正在尝试制作登录菜单。我创建了一个可更改的用户/通行证系统。但我想排除循环,要求他们再次输入用户名/密码?

以下是源代码:

print ("Welcome, please login.")

username = input("Please set a username")
password = input("Please set a password")

print ("please can you login")
username_input = input("What is your username?")
password_input = input ("What is your password?")

if username_input == username and password_input == password:
    while 1==1:
        print ("1. Change username")
        print ("2. Change password")
        print ("3. Go back to login menu")
        print ("4. Exit")
        option = int(input("Please select an option, 1/2/3/4?"))
        if option == 1:
            username = input("Enter a new username")
            print ("You username is now",username)

        elif option == 2:
            password = input("Enter a new password")
            print ("Your password is now",password)
        elif option == 3:
            #Help
        elif option == 4:

            quit()

2 个答案:

答案 0 :(得分:0)

你想离开循环,因此

break

是你的答案。你能更清楚一下你想要做什么吗?

答案 1 :(得分:0)

如果您仍需要答案:

Value = 2
While value == 2
print ("1. Change username")
print ("2. Change password")
print ("3. Go back to login menu")
print ("4. Exit")
option = int(input("Please select an option, 1/2/3/4?"))
if option == 1:
    username = input("Enter a new username")
    print ("You username is now",username)
    Value = 1
elif option == 2:
    password = input("Enter a new password")
    print ("Your password is now",password)
    Value = 1
elif option == 3:
    Pass
elif option == 4:

    quit()