如何对密码实施3try授权?

2024-10-02 22:33:44 发布

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

elif loginChoice=="Doctor" or loginChoice=="doctor":
    inputPass=str(input("Input doctor's password:(see line 1 of code) "))
    while True:
        if inputPass != adminPass:
            for tries in range (0,2):
                print("Wrong password! Only "+str(2-tries)+" tries left.")
                inputPass=str(input("Input doctor's password:(see line 1 of code) "))
                if inputPass==adminPass:
                    break;
                if tries==2:
                    break;
            break;
        else:
            print("Permission granted...")
            print(" ")

因此,我正在创建一种自我诊断程序,用于自我改进,并创建了一种医生登录方法,以便用户可以访问阵列中的数据。因此,我通过3次登录尝试向该登录添加了密码。然而,我意识到,如果我一次输入错误的密码,它会执行包含“仅错误尝试”循环的if循环,即使我第二次输入正确的密码,它也不会退出该循环并将我放入“else:”函数。有什么办法可以解决这个问题吗


Tags: of密码inputiflinepasswordprintdoctor
2条回答

让它更像这样怎么样:

adminPass = 'asdf' # just put this in for testing purposes
tries = 0
while True:
    inputPass=str(input("Input doctor's password:(see line 1 of code) "))
    if inputPass == adminPass:
        passwordCorrect = True
    else:
        passwordCorrect = False
        print("Wrong password! Only "+str(2-tries)+" tries left.")
        tries += 1

    if tries > 1:
        print("Wrong password! Permission denied.")
        break

    if passwordCorrect:
        print("Permission granted...")
        print(" ")
        break

不要让它变得如此复杂和混乱,只需创建一个变量,该变量可以跟踪用户的尝试,如果用户尝试次数为4次,则循环将自动停止尝试以下操作:

if loginChoice=="Doctor" or loginChoice=="doctor":
    #inputPass=str(input("Input doctor's password:(see line 1 of code) "))
    noOfattempts = 0
    while noOfattempts != 3:
        inputPass=str(input("Input doctor's password:(see line 1 of code): "))
        if inputPass != adminPass:
            # for tries in range (0,2):
            #     print("Wrong password! Only "+str(2-tries)+" tries left.")
            #     inputPass=str(input("Input doctor's password:(see line 1 of code) "))
            #     if inputPass==adminPass:
            #         break;
            #     if tries==2:
            #         break;
            # break;
            noOfattempts+=1
        else:
            print("Permission granted...")
            print(" ")

相关问题 更多 >