【问题标题】:Python - How to get out of a while loop?Python - 如何摆脱while循环?
【发布时间】:2016-05-22 19:28:11
【问题描述】:

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

这里是源代码:

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()

【问题讨论】:

  • break 离开那里。
  • 那是很大的缩进...
  • 破解不起作用?它只是退出程序。

标签: python loops while-loop


【解决方案1】:

你想跳出循环,因此

break

是你的答案。你能更清楚你到底想做什么吗?

【讨论】:

  • @HazardGames 然后你想将你的用户名/密码输入移动到循环中。
  • @HazardGames ,目前还不清楚您到底想做什么。您是在问我们,如果用户选择选择 3 ,他会再次回到开始,即主菜单所在的位置他必须输入他的用户名和密码?
【解决方案2】:

如果您仍然需要这个问题的答案:

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()

【讨论】:

    猜你喜欢
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 2014-09-05
    • 2022-12-18
    相关资源
    最近更新 更多