为什么在不正确的猜测之后此功能不会重新提示用户?

我一直在空闲时间写一段代码,忙于打开/关闭文件,以尝试获得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”中存储了一个单词)并有意输入错误的密码进行错误测试时,不会再次提示我输入其他密码并按原样打印我的尝试。


我确保在另一个函数中定义一个函数是可以接受的,并且我的拼写是正确的,并且在尝试使代码正常工作之后,我找不到问题了。。


富国沪深
浏览 141回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python