-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
思考函數而不是線條,它會更容易 – PRMoureu
循環自動執行。您正在使用'break'顯式退出循環。 – user2357112
循環塊應該縮進 – SwiftsNamesake