经过一定次数的尝试后,使用锁定的Python密码系统

经过一定次数的尝试后,使用锁定的Python密码系统,python,time,passwords,Python,Time,Passwords,因此,我试图用Python创建一个密码系统,在经过一定次数的错误尝试后,用户将被阻止访问它,比如说,5分钟。我目前不确定在重新运行同一个文件并以这种方式使用之后,如何保持变量的值。有人能帮我吗,因为我目前还不熟悉Python 更新: 在使用Jonas Wolff提供的代码进行了一段时间的实验后,我最终确定了以下代码: def password(): count = 0 currentTime = float(time.time()) passwordList = [

因此,我试图用Python创建一个密码系统,在经过一定次数的错误尝试后,用户将被阻止访问它,比如说,5分钟。我目前不确定在重新运行同一个文件并以这种方式使用之后,如何保持变量的值。有人能帮我吗,因为我目前还不熟悉Python

更新:

在使用Jonas Wolff提供的代码进行了一段时间的实验后,我最终确定了以下代码:

def password(): 
    count = 0 
    currentTime = float(time.time()) 
    passwordList = ["something", "password", "qwerty", "m42?Cd", "no"] 
    passwordNum = random.randint(0, 4) 
    password = passwordList[passwordNum] 
    with open("password.txt", "r") as file:
        check = file.readline()
        initialTime = file.readline()
        if initialTime=="":
            initialTime==0
    if int(check)==1 and (currentTime-float(initialTime))<300:
        print("You are still locked")
        print("Please try again in", int(300-(currentTime-float(initialTime))), "seconds.")
        quit()
    print("The randomised password is No.", passwordNum+1) #Prints a string to let the user know which password was randomly selected
    while count<5:
        inp = input("Enter the Password: ")
        if inp==password:
            print("Access Granted")
            print()
            f = open("password.txt", "w")
            f.write("0\n0")
            f.close()
            select()
        elif (count+1)==5:
            print("You have been locked")
            print("Please try again in 5 minutes")
            f = open("password.txt", "w")
            f.write("1\n")
            f.write(str(currentTime))
            f.close()
            quit()
        else:
            count+=1
            print("Incorrect Password")
            print("You have", 5-count, "tries left.")
            continue
def password():
计数=0
currentTime=float(time.time())
密码列表=[“某物”、“密码”、“qwerty”、“m42?Cd”、“否”]
passwordNum=random.randint(0,4)
password=passwordList[passwordNum]
打开(“password.txt”、“r”)作为文件:
check=file.readline()
initialTime=file.readline()
如果initialTime==“”:
初始时间==0
如果int(检查)==1和(当前时间浮动(初始时间))
这行吗?
只需确保您有一个名为PhysxInit.txt的文本文件

运行程序后,我猜错了几次,我的txt文件是这样的

11111
1536328469.9134998
它看起来应该和我的差不多,尽管数字可能不同

要按要求阅读特定行,您需要执行以下操作:

with open("PhysxInit.txt", "r") as f:
    for w,i in enumerate(f):
        if w == #the number line you want:
            # i is now the the line you want, this saves memory space if you open a big file

请出示您的密码。您是否希望在问题运行之间将某种信息存储在文件中?也许您可以使用timeit模块,或者您希望在程序关闭时(如您之前所写)存储尝试?是,我确实希望即使在程序关闭后也能存储该尝试,这样,如果每次密码输入错误后重新运行,则在“锁定”持续时间结束之前,用户将无法输入密码。我试过用pickle来做这个,但仍然不确定它是如何工作的。会有更简单的方法吗,比如JavaScript中的sessionStorage?请注意,我试图仅在Python中实现这一点(因为我认为如果使用SQL或类似数据库,这是可能的)。如果做不到,就告诉我。谢谢。现在修复它,它将读取您已经尝试了多少次您的代码,但在输入密码后,它会无限期地要求您输入密码。你的代码中有没有什么我需要调整的地方,以便它能与我的程序一起工作(我有一个名为PhysxInit的txt文件)?记住,如果你没有将它输入到你的锁定时间的5倍,由于它将其保存在文本文件中。另一方面,它将失败尝试的次数存储为PhysxInit.txt文件中的次数。您通过手动删除这些尝试来重置它,如果您在前5次尝试中尝试猜测正确,它将自动删除这些尝试,并且它将忘记您曾经猜错过的次数,但是您是否错误了5次,然后没有自动返回的方法,因为我可以理解,您希望在所说的时间内发生这种情况,如果您在第一行上写入“\n”的某个时间戳已经过去,则可以这样做。我已经编辑了代码,它现在似乎可以工作,因此它只会将您锁定5分钟,而不是永远
with open("PhysxInit.txt", "r") as f:
    for w,i in enumerate(f):
        if w == #the number line you want:
            # i is now the the line you want, this saves memory space if you open a big file