Python:被要求输入两次并输出重复的打印语句

标签 python list function while-loop conditional-statements

我正在尝试添加一个菜单来完成我的程序,该菜单允许用户选择一些允许用户在列表中存储网站名称和密码的选项。但是,当我将一些网站名称和密码附加到各自的保管库中时,当我尝试在附加网站名称和密码后选择一个选项时,就会出现一个问题,例如“1”是调用 viewapp 的预期输入( )功能可以查看迄今为止存储的网站和密码。问题是调用 viewapp() 函数需要两次以上,它拒绝第一个预期输入,但奇怪地接受第二个输入。此外,当我选择第三个选项来调用summary()时,整个打印的摘要将打印两次,这与仅接受第二个预期输入的菜单类似。该程序正在做我想要的事情,除了这个恼人的错误,选择这四个选项会使其在应该立即跳转到该功能时再次要求输入。如有帮助,我们将不胜感激。

appvault = []
passvault = []

def logged():
    print("----------------------------------------------------------------------\n")
    print("Hello, welcome to the password vault console. ")
    modea = input("""Below are the options you can choose from in the password vault console:
    ##########################################################################\n
    1) Find the password for an existing webiste/app
    2) Add a new website/app and a new password for it
    3) Summary of the password vault
    4) Exit
    ##########################################################################\n
    > """).strip()
    return modea

def viewapp():
    if len(appvault) > 0:
        for app in appvault:
            print("Here is the website/app you have stored:")
            print("- {}\n".format(app))
    if len(passvault) > 0 :
        for code in passvault:
            print("Here is the password you have stored for the website/app: ")
            print("- {}\n".format(code))

    else:
        print("You have no apps or passwords entered yet!")

def addapp(): 
    while True:
        validapp = True
        while validapp:
            new_app = input("Enter the new website/app name: ").strip().lower()
            if len(new_app) > 20:
                print("Please enter a new website/app name no more than 20 characters: ")
            elif len(new_app) < 1:
                print("Please enter a valid new website/app name: ")
            else:
                validapp = False
                appvault.append(new_app)

        validnewpass = True
        while validnewpass:
            new_pass = input("Enter a new password to be stored in the passsword vault: ")
            if not new_pass.isalnum():
                print("Your password for the website/app cannot be null, contain spaces or contain symbols \n")            
            elif len(new_pass) < 8:
                print("Your new password must be at least 8 characters long: ")
            elif len(new_pass) > 20:
                print("Your new password cannot be over 20 characters long: ")   
            else:
                validnewpass = False
                passvault.append(new_pass) 

        validquit = True
        while validquit:
            quit = input("\nEnter 'end' to exit or any key to continue to add more website/app names and passwords for them: \n> ")
            if quit in ["end", "End", "END"]:
                logged()
            else:
                validquit = False
                addapp()
            return addapp        

def summary():
    if len(passvault) > 0:
        for passw in passvault:
            print("----------------------------------------------------------------------")
            print("Here is a summary of the passwords stored in the password vault:\n")
            print("The number of passwords stored:", len(passvault))
            print("Passwords with the longest characters: ", max(new_pass for (new_pass) in passvault))
            print("Passwords with the shortest charactrs: ", min(new_pass for (new_pass) in passvault))
            print("----------------------------------------------------------------------")
    else:
        print("You have no passwords entered yet!")

while True:        
    chosen_option = logged()
    print(chosen_option) 
    if chosen_option == "1":
        viewapp()

    elif chosen_option == "2":
        addapp()   

    elif chosen_option == "3":
        summary()

    elif chosen_option == "4":
        break
    else:
        print("That was not a valid option, please try again: ")

print("Goodbye")

最佳答案

发生这种情况是因为您在退出 addapp() 时调用了 logged():

if quit in ["end", "End", "END"]:
    logged()

然后,您输入的选择将由 logged() 返回,并被丢弃,因为它没有分配给任何东西。

您现在回到了 addapp() 中上一个 block 的末尾,下一条指令是 return addapp,它将带您返回主程序循环,您将被 chosen_option =logging()

再次发送到 logged()

请注意,在 return addapp 中,您返回了 addapp function 本身,这当然不是您想要做的。因此,由于 addapp() 不需要返回值,只需使用 return,或者什么都不用,Python 将在函数末尾自动返回。

因此,要解决您的问题:输入网站后直接返回:

if quit in ["end", "End", "END"]:
    return

另请注意,当您添加更多站点时,您会从自身递归调用 addapp()
通常应该避免这种情况,除非您确实想使用某种递归算法,而应该像在主循环中那样使用循环。默认情况下,Python 将您的递归级别限制为 1000 个 - 因此您甚至可能会因为连续输入超过 1000 个站点而导致应用程序崩溃;)

摘要问题只是由 summary() 中不必要的 for 循环引起的

关于Python:被要求输入两次并输出重复的打印语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44716362/

相关文章:

python - 为什么 pip 不起作用?

具有多个自变量的 Python curve_fit

python - wxpython - 删除背景删除非背景组件

javascript - 通过java API从orientdb调用JS函数

c - 嵌套函数的主体保存在哪里?

python - 反转大型 JSON 字典

python - 使用Python将包含一些关键字的字符串分成列表

Python: "If"函数

python - 如何线性组合 python 中的列表列表,其中不是每个项目都是列表?

java - 调用performClick()函数时什么也没得到 - Android