在程序中,用户必须输入密码,如果输入错误,程序将再提示他两次,如果两次错误,程序将过期。我该怎么做呢?这是我到目前为止的程序...
password=input("Enter password:")
while password != 'cat':
print ("password is wrong, try it again")
password= input ("Enter your password:")
print ("Password correct be happy")
最佳答案
慈善程序员
for trial in range(3):
print ("Attempt no.",trial, end=" ")
passw = input('. Enter password > ')
if passw == 'cat' : break
else:
passw = 'cat'
是的,简单的 for
循环也有一个 else
子句:"Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement." .
关于Python:如何让程序在用户输入时提示用户一定的固定次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27063289/