显示整个列表,用户可以在其中进行交互

时间:2019-04-03 05:41:58

标签: python python-3.x

我的程序旨在在用户输入应用程序密码时向其显示密码,但是由于某些原因,如果用户先前输入了信息,则该程序不会在此处显示print('''here are your apps, {} which ones information do you want to view'''.format(a[0])行中输入的所有应用程序名称对于youtube,facebook等,它应该打印出print('''``有您的应用程序, Youtube,Facebook 您想查看哪些信息'''.format(a [0])) 然后用户将键入wich并显示继承密码。 请不要在这里降职,并且在询问:)之前已尝试修复。

vault_apps = []           

users_passwords = ""
def existing_apps(): 
    if len(vault_apps) < 1:
        print('''you have currently 0 app and passwords stored on your account''')
        locker_menu_func()
    else:
        for a in vault_apps:
            print('''here are your apps, {}
which ones information do you want to view'''.format(a[0]))
            break

        while True: 
            users_passwords = input('''
''')
            if users_passwords == "":
                print('''Please enter a valid answer''')
            else:
                for a in vault_apps:
                    if users_passwords in a:
                        print('''{}
password: {}'''.format(users_passwords, a[1]))


def store_apps(): 
            while True: 
                        app_name = input('''What is the name of the website/app your are adding?
''') 
                        if 0 < len(app_name) < 16:
                                    break
                        elif app_name == "":
                                    print("Please enter an answer")
            while True:
                        app_password = input('''What is the password of your {} account?
'''.format(app_name))                        
                        if app_password == "":
                                    print("Please enter an answer")
                        else: vault_apps.append([app_name, app_password])
                        break
            while True:
                        add_app = input('''would you like to add another app and password, yes or no
''')

                        if add_app.lower() == "no":
                            locker_menu_func()
                        elif add_app.lower() == "yes":
                            store_apps()
                        else: 
                            print("please enter a proper answer")






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
3) see a summary of your password locke \n4) exit password locker successfully
---------------------------------------------------------------------------------
''')
            print('''----------------------------------------------------------------''')    
            while True:
                        if locker_menu_var == "1": existing_apps()


                        if locker_menu_var == "2": store_apps()
                        locker_menu_func()
                        break
locker_menu_func()

2 个答案:

答案 0 :(得分:0)

问题出在您的existing_apps方法中。您正在遍历vault_apps,但是在打印第一个值后break正在迭代。

我添加了一个修改,其中我们使用列表理解来获取名称列表,使用join()将其加入,然后打印值。

还要注意一个不错的格式化技巧,您可以使用f"{some_variable}"代替"{}".format(variable)

def existing_apps(): 
    if len(vault_apps) < 1:
        print('''you have currently 0 app and passwords stored on your account''')
        locker_menu_func()
    # PLEASE LOOK AT THE BELOW PORTION
    else:
        app_names = [x[0] for x in vault_apps]
        app_names_pretty = ", ".join(app_names)
        print(f'here are your apps, {app_names_pretty} | which ones information do you want to view')
        # END OF MODIFICATION 
        while True: 
            users_passwords = input()
            if users_passwords == "":
                print('''Please enter a valid answer''')
            else:
                for a in vault_apps:
                    if users_passwords in a:
                        print('''{}password: {}'''.format(users_passwords, a[1]))

答案 1 :(得分:0)

  1. break正在这里找到的第一个app

..

for a in vault_apps:
     print('''here are your apps, {}which ones information do you want to view'''.format(a[0]))
     break
  1. 您不会检查用户输入无效的app名称(此处不存在密码)的情况:

..

 for a in vault_apps:
      if users_passwords in a:
           print('''{}password: {}'''.format(users_passwords, a[1]))
  1. 您还需要检查无效的应用名称:

..

if any(app_name in sl for sl in vault_apps):
  1. 如果用户不想继续操作,则需要在其中放置quit语句:

..

app_name = input("Enter the app name to view its password or Q to quit: ")
if app_name.lower() == "q" : exit("Thank you, see you again!")

因此

def existing_apps():
if len(vault_apps) < 1:
    print("you have currently 0 app and passwords stored on your account")
    locker_menu_func()
else:
    print("here are your apps, {}".format([str(x[0]) for x in vault_apps]))
    while True:
        app_name = input("Enter the app name to view its password or Q to quit: ")
        if app_name.lower() == "q" : exit("Thank you, see you again!")
        else:
            if any(app_name in sl for sl in vault_apps):
                for a in vault_apps:
                    if app_name in a: print("{} password: {}".format(app_name, a[1]))
            else: print("Invalid app name!")

输出

What is the name of the website/app your are adding?facebook
What is the password of your facebook account?face123
would you like to add another app and password, yes or noyes
What is the name of the website/app your are adding?stackoverflow
What is the password of your stackoverflow account?stack123
would you like to add another app and password, yes or nono
You have opened the locker, 
Please select what you would like to do,
Press: 
1) find your existing passwords 
2) save a new password for your apps
3) see a summary of your password locke 
4) exit password locker successfully
---------------------------------------------------------------------------------
1
----------------------------------------------------------------
here are your apps, ['facebook', 'stackoverflow']
Enter the app name to view its password: stackoverflow
stackoverflow password: stack123
Enter the app name to view its password: facebook
facebook password: face123
Enter the app name to view its password: myspace
Invalid app name!
Enter the app name to view its password or Q to quit: q
Thank you, see you again!