显示整个列表,用户可以在其中与

2024-05-19 02:12:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我的程序是为了向人们显示他们输入应用程序的密码,但由于某些原因,它不会显示他们在print('''here are your apps, {} which ones information do you want to view'''.format(a[0])行中输入的所有应用程序名称。如果用户以前输入过他们在youtube、facebook等网站的信息,它应该打印出来('''有你的应用,youtube、facebook 要查看哪些信息“”。格式(a[0])) 然后用户将键入其中一个并显示其密码。 请不要拒绝我在这里仍然是新的,并试图修复之前问:)。你知道吗

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

Tags: appsnameyouappyourifpasswordusers
2条回答
  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! 

问题出在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]))

相关问题 更多 >

    热门问题