我如何使我的if语句真正做某事

2024-06-28 20:31:48 发布

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

def attempt_function():
    number_of_attempts = 1
    saidpassword = ""
    if number_of_attempts > 3:
        print("You have entered the wrong password too many times, please try again later")
    saidpassword = input("the password you entered is incorrect, please try again\n")
    if saidpassword != password:
        number_of_attempts + 1
        saidpassword = input("the password you entered is incorrect, please try again\n")

当我使用所有的尝试时,if number_of_attempts > 3:似乎不起作用。它基本上不打印


Tags: oftheyounumberinputifispassword
2条回答

在没有看到更多代码的情况下,不是100%确定,但是在尝试密码后是否调用了“尝试函数”?在顶部设置尝试次数=1,因此它从1->;2然后重置为1

def attempt_function():
    number_of_attempts = 0
    saidpassword = ""
    
    while saidpassword != password:
        number_of_attempts + 1
        saidpassword = input("the password you entered is incorrect, please try again\n")
        if number_of_attempts > 3:
            print("You have entered the wrong password too many times, please try again later")
            break
        saidpassword = input("the password you entered is incorrect, please try again\n")

您需要一个while循环来增加尝试次数

相关问题 更多 >