我正在尝试完成我的程序我添加了一个菜单,允许用户选择一些允许用户在列表中存储网站名称和密码的选项。但是,只要我将一些网站名称和密码添加到各自的保险库中,当我在添加网站名称和密码后尝试选择一个选项时就会出现问题," 1"例如,调用viewapp()函数以查看到目前为止存储的网站和密码的预期输入。问题是调用viewapp()函数需要两次以上,它拒绝第一个预期的输入,但奇怪地接受第二个输入。此外,当我选择第3个选项以调用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")
答案 0 :(得分:2)
这是因为您在退出logged()
时致电addapp()
:
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多个站点来使应用程序崩溃;)
摘要问题仅由for
summary()
循环引起
答案 1 :(得分:1)
你快到了。问题出在第63行的addapp()函数中:
if quit not in ["end", "End", "END"]:
logged()
如果你更换
logged()
与
pass
然后一切都会好起来的。 无论如何,您无法在此处理已记录函数的结果。 您也不需要在此处理记录的功能。 addapp将退出,并且将在调用addapp函数的while循环中调用和处理记录的函数。