2016-05-22 149 views
-4

我正在嘗試創建登錄菜單。我創建了一個可更改的用戶/通行證系統。但是我想要退出循環,要求他們再次輸入他們的用戶名/密碼?Python - 如何擺脫while循環?

這裏是源代碼:

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

'break'離開那裏。 – miradulo

+0

那些是非常大的縮進... –

+0

打破不起作用?它只是退出程序。 – HazardGames

回答

0

你想走出循環,因此

break 

是你answer.Can你多一點明確的正是你想要什麼做?

+0

讓它再次詢問用戶的通行證和用戶,但我不確定... – HazardGames

+1

@HazardGames然後,您要將用戶名/密碼輸入移動到循環中。 –

+0

@HazardGames,它仍然不清楚你確實想做什麼。你問我們,如果用戶去選擇3,他又被帶回到開始,那是他必須輸入他的用戶名的主菜單,密碼? –

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