在 Python 中浏览我的循环时遇到问题

用户11296864

我已经问过我周围的人并无数次尝试获得这个修复程序。该程序应该能够根据需要多次添加用户的网站和密码,并显示他们选择的网站和密码。

现在,当你回答would you like to add another website?它不到风度再要求一个新的网站名称和密码,但只是重复的问题would you like to add another website?,还当你在一个网站的名称和密码,答案已经进入would you like to add another website?则选择选项1,看现有的帐户,它重复would you like to add another website?,当这甚至应该出现在选项一时

输入以及它应该如何输出:

1) 找到您现有的密码
2) 为您的应用程序保存一个新密码
3) 查看您的密码柜摘要
4) 成功退出密码柜1

您没有存储的网站和密码

1) 找到您现有的密码
2) 为您的应用程序保存一个新密码
3) 查看您的密码柜摘要
4) 成功退出密码柜2

您要添加的网站/应用程序的名称是什么?Instagram

您的 {instagram} 帐户的密码是什么?鲍勃91

您想添加另一个网站吗?是的

您要添加的网站/应用程序的名称是什么?Facebook

您的 {facebook} 帐户的密码是什么?鲍勃92

您想添加另一个网站吗?

1) 找到您现有的密码
2) 为您的应用程序保存一个新密码
3) 查看您的密码柜摘要
4) 成功退出密码柜1

输入您要查找Instagram密码的应用程序网站
名称
= Instagram 密码 = bob91

完整代码:

vault_apps = []
app_name = ""
def locker_menu_func():
    print('''You have opened the locker, 
Please select what you would like to do,''')
    locker_menu_var = input('''Press: \n1) find your existing passwords \n2) save a new password for your apps\n3) see a summary of your password locke \n4) exit password locker successfully\n---------------------------------------------------------------------------------
    ''')
    print('''----------------------------------------------------------------''')
    while True:
        if locker_menu_var == "1":
            while len(vault_apps) < 1:
                print('''you have nothing stored''')
                if len(vault_apps) > 1:
                    print(vault_apps)

                break
            break
        if locker_menu_var == "2":
            app_name = input('''
What is the name of the website/app your are adding?
''')
            app_password = input('''What is the password of your {} account?
'''.format(app_name))
            vault_apps.append([app_name, app_password])
            while True: 
                ask_again = input('''Would you like to add another app and password? 
            ''')
                if ask_again.lower() == "yes":
                    locker_menu_var = "2"
                elif ask_again.lower() == "no":
                    locker_menu_func()


                else:
                    print("please enter a valid response") #should repeat if user want to add another website
奥哈斯莫里尔

这个逻辑是有缺陷的。一旦进入无限while循环,就无法退出,只能输入“no”。当您输入“yes”时,locker_menu_var 的值会发生变化,但您不会退出循环,因此它会不断重复相同的菜单。

            while True:     
            ask_again = input('''Would you like to add another app and password? 
        ''')
            if ask_again.lower() == "yes":
                locker_menu_var = "2"
            elif ask_again.lower() == "no":
                locker_menu_func()

您正在混合循环和递归,这让事情变得混乱。一种简单的方法是:

vault_apps = []

def locker_menu():

    # the entry message
    msg = '''You have opened the locker, Please select what you would like to do,'''
    print(msg, end="\n\n")

    # nume options
    menu = ["1) find your existing passwords", 
    "2) save a new password for your apps", 
    "3) see a summary of your password locker", 
    "4) exit password locker successfully"]
    menu_string = f"Press:\n{menu[0]}\n{menu[1]}\n{menu[2]}\n{menu[3]}\n"

    # now enter the loop
    while True:

        # input variable
        # NOTE: This variable is inside the loop, 
        # so that the user can enter the value everytime
        # the loop repeates.
        locker_menu_var = input(menu_string)

        if locker_menu_var == "1":
            # retrieve password for an existing record.
            # although it's not a good idea to just print
            # all the records, I am not changing your application logic
            # because I don't see any use in it.

            # you missed one case in your logic,
            # which I have fixed here.
            if len(vault_apps) == 0:
                print("you have nothing stored")
            else:
                print(vault_apps)

        elif locker_menu_var == "2":
            # for a new entry
            # enter your logic here
            an = input("app name: ")
            passw = input("password: ")
            vault_apps.append([an, passw])
            done = False # flag for exiting
            while not done:
                inp = input("enter another?")
                if inp == "yes":
                    # enter logic here
                    an = input("app name: ")
                    passw = input("password: ")
                    vault_apps.append([an, passw])
                else:
                    done = True
        elif locker_menu_var == "3":
            # do something
            pass
        elif locker_menu_var == "4":
            return          

if __name__ == "__main__":
    locker_menu()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章