简体   繁体   English

为什么这个函数在猜错之后没有重新提示用户?

[英]Why does this function not re-prompt the user after an incorrect guess?

I have been writing a piece of code in my free time, messing around with opening/closing files to try and get a 100% secure file. 我在空闲时间写了一段代码,搞乱打开/关闭文件以尝试获得100%安全的文件。

I have this so far: 到目前为止我有这个:

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()

For some reason, when I run this (with a word already stored in "pass.txt") and intentionally type in the wrong password for bug testing, I am not re-prompted to enter another password with my attempts printed as it should. 出于某种原因,当我运行它(已经存储在“pass.txt”中的单词)并故意输入错误的密码进行错误测试时,我不会再次提示输入另一个密码,我的尝试打印应该是这样。

I have made sure that defining a function inside of another function is acceptable and my spelling is correct, and after playing around with the code trying to get it to work I am not able to find the problem. 我已经确保在另一个函数中定义一个函数是可以接受的并且我的拼写是正确的,并且在使用代码试图让它工作之后我无法找到问题。 .

I don't see where LockMech is ever being called. 我没有看到LockMech被称为何处。

Having LockMech call itself recursively is an odd way to approach retries. LockMech递归调用本身是一种奇怪的方法来进行重试。 Why not use a while loop or a for loop? 为什么不使用while循环或for循环?

The preferred way to read the passcode is to use a context manager 读取密码的首选方法是使用上下文管理器

with open("pass.txt",  "r+") as my_pass:
    passcode = my_pass.read()

This way the file is closed automatically at the end of the block 这样,文件在块结束时自动关闭

Here is a simplified version of your PasswordLock . 这是您的PasswordLock的简化版本。 You'll have to make some other small changes to your program to use it 您必须对程序进行一些其他小的更改才能使用它

def PasswordLock():
    with open("pass.txt",  "r+") as my_pass:
        passcode = my_pass.read()

    for attempts_remaining in (2, 1, 0):
        x = input("Password: ")
        if x == passcode:
            print("Passcode was correct. Opening secure files...")
            return True
        if attempts_remaining:
            print("Passcode was not corrent, please try again.")
            print("Attempts Left: {}".format(attempts_remaining)

    print("You are out of attempts, access restricted.")
    return False

Notice that I replaced your call to Close() with a return False . 请注意,我将您的调用替换为Close()return False Function calls are not the same as "goto". 函数调用与“goto”不同。 You'll get yourself into bother if you keep trying that 如果你继续这样做,你会让自己烦恼

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 输入范围无效后如何重新提示循环用户输入 - How to re-prompt loop user input after invalid input range 如何处理 ValueError 并在 Python 中重新提示用户特定问题? - How to deal with a ValueError and re-prompt the user a specific question in Python? 使用相同的变量重新提示用户输入 - re-prompt input from the user using the same variable 如何在不重新提示凭据的情况下运行长Python脚本 - How to run long Python script without re-prompt for credentials 如何在 Python 中重新提示输入并修复高/低数字猜谜游戏中的无限循环 - How to re-prompt input in Python & fix infinite loop in high/low number guessing game 为什么在Python 2.7中两次提示用户? - Why does it prompt the user twice in Python 2.7? 无法猜测为什么重载函数实现不接受所有可能的参数 - Cannot guess why Overloaded function implementation does not accept all possible arguments 为什么在函数中使用模块后重新导入会引发异常? - Why does RE-importing after a module is used in a function throw an exception? 为什么python代码在所有情况下都会导致用户不正确? - Why does the python code result in the user being incorrect in all circumstances? 包括该函数的循环,以便在数字不正确时要求用户重新输入数字 - including a loop for the function so it can ask the user to re enter a number if the number is incorrect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM