所以我尝试用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
非常感谢你的帮助和耐心回答我的问题。在
这行吗? 只要确保你有一个名为物理初始化.txt在
运行程序后,又猜错了几次,我的txt文件是这样的。在
^{pr2}$它应该看起来像我的,虽然数字可能不同。在
要按您的要求阅读特定行,您需要执行以下操作:
相关问题 更多 >
编程相关推荐