【问题标题】:Python: Being asked for input twice and outputting duplicate print statementsPython:被要求输入两次并输出重复的打印语句
【发布时间】:2017-11-26 17:37:34
【问题描述】:

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

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

【问题讨论】:

    标签: python list function while-loop conditional-statements


    【解决方案1】:

    这是因为您在退出addapp() 时调用了logged()

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

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

    你现在回到addapp() 中上一个块的末尾,下一条指令是return addapp,这将带你回到你的主循环,在那里你将再次被发送到logged() chosen_option = logged()

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

    所以,为了解决你的问题:当你输入完站点后直接return

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

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

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

    【讨论】:

      【解决方案2】:

      你快到了。问题出在第 63 行的 addapp() 函数中:

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

      如果你替换

      logged()
      

      pass
      

      然后一切都会好起来的。 无论如何,您都没有在此处处理记录函数的结果。 您也不需要在此处处理记录的功能。 addapp 将退出,记录的函数将在调用 addapp 函数的 while 循环中被调用和处理。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-03
        • 1970-01-01
        • 2021-09-03
        • 1970-01-01
        • 1970-01-01
        • 2015-12-29
        相关资源
        最近更新 更多