gpt4 book ai didi

Python 密码系统在尝试一定次数后会锁定

转载 作者:行者123 更新时间:2023-12-01 01:38:15 24 4
gpt4 key购买 nike

因此,我尝试在 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

非常感谢您提供的帮助以及您耐心回答我的问题。

最佳答案

import YourProgram # this is the program you want to run, if the program runs automaticly when opened then move the import to the part where i wrote YourProgram() and delete the YourPregram() line
import time

pswd = "something"

count = 0

with open("PhysxInit.txt","r") as file:
file_info = file.readline()
numa = file_info.count("1")
count = numa


while True:
with open("PhysxInit.txt","r") as file:
file_info = file.readline()
tima = file.readline()


inp = input("What is the password:")



if inp == pswd:

if tima == "":
tima = "0" # this should solve yoúr problem with float convertion however it doesn't make sence that this step should be needed


if str(file_info[:5]) != "11111" or time.time() > float(tima):
YourProgram() # this is just meant as the thing you want to do when when granted acces i magined you where blocking acces to a program.
f = open("PhysxInit.txt", "w")
f.write("\n")
f.close()
break
else:
count += 1
f = open("PhysxInit.txt", "w")
f.write(("1"*count)+"\n"+str(tima))
if count == 5:
f.write(str(time.time()+60*5))
f.close()

#f = open("PhysxInit.txt", "w")
#f.write("\n")
#f.close()

这个有用吗?只需确保您有一个名为 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

关于Python 密码系统在尝试一定次数后会锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52177889/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com