如何返回到循环Python中的第一行代码

2024-05-18 23:40:47 发布

您现在位置: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

Tags: the代码程序密码if过程错误方式
1条回答
网友
1楼 · 发布于 2024-05-18 23:40:47

最后一条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()

相关问题 更多 >

    热门问题