密码系统。如果猜测正确,如何允许程序继续进行3次错误猜测

时间:2016-08-11 09:58:16

标签: python loops passwords

程序需要允许用户3错误地尝试写入密码。但是,如果他们让密码尝试正确,我希望程序能够打印出来并且很好。

以下是我目前的情况:

def main():

    n = 2

    for i in range(0, 3, 1):

        attempt = input('Enter password: ')


        if attempt != 'password':
            print('Incorrect. ' + str(n) + ' attempts left')

        n = int(n) - 1

        else:
            print('nice')

3 个答案:

答案 0 :(得分:1)

max_retries = 3
for i in range(max_retries):
    passwd = input('\nEnter password: ')
    if passwd == 'password':
        print('\nnice')
        break
    print('Incorrect.  %d attempts left' % (max_retries-i-1))

答案 1 :(得分:-1)

n = 2
for i in range(0, 3, 1):
    attempt = raw_input('Enter password: ')
    if attempt != 'password':
        print('Incorrect. ' + str(n) + ' attempts left')
        n = int(n) - 1
    elif n > 0:
        print('nice')
    else:
        print('no attempts left')

答案 2 :(得分:-1)

for x in range(3):
pass_correct=False
password = "password"
attempt=raw_input("Enter password: ")
if attempt == password:
     pass_correct=True
     break
else:
    print "incorrect, " + str(2-x) + " attempts left"
if pass_correct:
    print "nice"
else:
    print "no attempts left"

简洁