如何在密码中显示计数:

时间:2013-12-09 20:14:46

标签: python

for attempt in range(5):                        
    password = input("Password: ")              
    if password == "changeme":
        print("Thou Shall Pass Into Mordor")
        break
else:
    print("Thou Shall Not Pass Into Mordor")

我需要帮助,因此会显示尝试密码,如下所示:

  

密码(1次尝试):

3 个答案:

答案 0 :(得分:3)

您的attempt变量包含尝试次数。将它与for循环中的print语句连接起来。

修改:删除添加1,没有必要。

答案 1 :(得分:0)

password_attempt = 0
for attempt in range(5):                        
    password = input("Password: ")              
    if password == "changeme":
        print("Thou Shall Pass Into Mordor")
        break
else:
    password_attempt += 1
    print("Thou Shall Not Pass Into Mordor")
    print(Password(%s attempt(s)) %password_attempt

答案 2 :(得分:0)

我只是对您发布的代码进行了一些更改,以满足您的需求。

for attempt in range(1, 5):                                # Change range to be 1 - 5.
    password = input("Password (%d attempts): " % attempt) # Change input message showing attempt number.             
    if password == "changeme":
        print("Thou Shall Pass Into Mordor")
        break
else:
    print("Thou Shall Not Pass Into Mordor")