2017-07-13 47 views
-1

我正在嘗試編寫一段簡單的python代碼塊,我希望在未來的程序中重複使用該代碼塊。它將檢查密碼是否正確以及是否不返回到第一行代碼並再次啓動該過程。我試圖完成的方式給了我一個錯誤。誰能幫我找到更好的解決方案?這是我目前使用的代碼:如何返回到循環中的第一行代碼Python

def password_checker(): 
    program_acceptance = "Welcome to the Program! " 
    acceptable_password = "Godisgood" 
print("Please Enter the Password") 
while True: 
password = input("Password: ") 
if password == acceptable_password: 
    print(program_acceptance) 
    break 
if password != acceptable_password: 
    print("Invalid password, please try again..." 
    break 
+1

思考函數而不是線條,它會更容易 – PRMoureu

+2

循環自動執行。您正在使用'break'顯式退出循環。 – user2357112

+0

循環塊應該縮進 – SwiftsNamesake

回答

1

應刪除最後break聲明,以確保程序保持循環時提供虛假的密碼。

def password_checker(): 
    program_acceptance = "Welcome to the Program! " 
    acceptable_password = "Godisgood" 
    print("Please Enter the Password") 
    while True: 
     password = input("Password: ") 
     if password == acceptable_password: 
      print(program_acceptance) 
      break 
     if password != acceptable_password: 
      print("Invalid password, please try again...") 

password_checker() 
+0

你應該提供一個解釋,而不是簡單的代碼。 – Darthfett