我正在制作一个密码系统,如何使第一个IF语句再次运行,而不将其转换为函数?

2024-09-29 02:16:29 发布

您现在位置:Python中文网/ 问答频道 /正文

if userInput != userPassword:
   print("I'm sorry, that is wrong please try again. Try to remember capitals!")
elif userInput == userPassword:
    print("That is correct you may now find out your mobile phone costs!")
    mobile_phone()

当运行时,我输入了错误的密码,它只是重复第一个打印语句(对不起,这是错误的,请再试一次)。尽量记住大写字母!)我如何才能让它只是再次运行一次,如果它是错误的,那么也许是另一个时间,直到它回答了正确的密码。首次登场


Tags: 密码ifthatis错误phonemobileprint
1条回答
网友
1楼 · 发布于 2024-09-29 02:16:29

把它放在一个循环中?你知道吗

while (userInput != userPass) :
    print ("wrong password")
    // reask userPass

如果你想限制try的数量,比如说3,只需添加一个计数器:

while (userInput != userPass && counter < 3) :
    counter++
    print ("wrong password")
    // reask userPass

相关问题 更多 >