我一直在空闲时间编写一段代码,搞乱打开/关闭文件以尝试获得 100% 安全的文件。
到目前为止我有这个:
def StartLock():
my_pass = open("pass.txt", "r+")
passcode = my_pass.read()
my_pass.close()
# Establish "passcode" variable by reading hidden text file
if passcode != "":
PasswordLock(input("Password: "))
elif passcode == "": #Passcode is empty
print("Passcode not set, please set one now.")
my_pass = open("pass.txt", "r+")
passcode = my_pass.write(input("New Pass: ")) #User establishes new pass
my_pass.close()
print("Passcode set to :" + passcode)
PasswordLock(passcode) #User is passed on with correct pass to the lock
def PasswordLock(x):
my_pass = open("pass.txt", "r+")
passcode = my_pass.read()
my_pass.close()
attempts = 3
def LockMech(x): #Had to do this to set attempts inside instance while not resetting the num every guess
if attempts != 3:
print("Attempts Left: " + str(attempts))
if x == passcode:
print("Passcode was correct. Opening secure files...")
return True
elif attempts == 0:
print("You are out of attempts, access restricted.")
Close()
elif x != passcode and attempts > 0:
print("Passcode was not corrent, please try again.") #This does get printed to console when I type in a wrong pass, so it gets here
attempts = attempts - 1
LockMech(input(":")) #This is what seems to be broken :(
def Close():
pass
StartLock()
出于某种原因,当我运行它(在“pass.txt”中已经存储了一个单词)并故意输入错误的密码进行错误测试时,我不会再次提示我输入另一个密码,并按应有的方式打印我的尝试。
我已经确保在另一个函数中定义一个函数是可以接受的并且我的拼写是正确的,并且在尝试让它工作的代码之后,我无法找到问题所在。.