密码库问题,if语句后没有代码

时间:2019-04-05 07:03:25

标签: python python-3.x

我的程序用于分配密码库,因此用户创建一个帐户并输入密码库,然后他添加应用程序和密码并查看它,问题是它还必须打印摘要,但是当用户没有输入应用程序和密码,它不会告诉他们他们没有任何应用程序。

摘要代码>>

def app_summary():
    while True:
        if len(vault_apps) < 1:
            print('''there is no summary of your acount as, 
you currently have 0 apps and passwords stored on your account''')  


        else: print('''You have {} app and its information stored'''.format(len(vault_apps)))
        print('''Your longest password  is charecters''', max(len(a) for a in vault_apps))
        print('''Your shortest password  is charecters''', min(len(a) for a in vault_apps))
        goback_menu()

        break 

您可以看到是否存储了<个应用程序,它应该打印出('''there is no summary of your acount as, you currently have 0 apps and passwords stored on your account'''),但我却收到此错误:max(len(a) for a in vault_apps)) builtins.ValueError: max() arg is an empty sequence

完整代码--->

vault_apps = []   
users_passwords = ""
username = ""
vault_password = ""

def exit_code(): #This funnction exits the code, and is made into a function as many times through the program I need to exit the code, and now I can just call multiple times through this unction
    while True:
        print('''\n\n\n\nYou have succesesfuly and safely logged out of password locker
Goodbye!''')
        exit() #this is a function for exit it exits the code, but on wing101 it takes you to an exit page which is made by the program called 'sitebulletins'


def goback_menu(): # this code allows the user to go to the main menue of the vault, and also exit at the same time, it was made so instead of always typing the main menue i can call it
    goback = input('''Would you like to go back to the main menu or exit the code?, answer with
yes, or exit
''')
    if goback == "yes":
        locker_menu_func()

    elif goback == "exit":
        print("okay")
        exit_code()

    else: print('''please enter yes, or no''')

def existing_apps(): #this function correspons, to the main menu when the user wants to viw their stored apps and passwords, and if the user has none it asks them if they would like to go back to the menu to add some
    if len(vault_apps) < 1:
        print('''you have currently 0 app and passwords stored on your account''')
        goback_menu()
    else:
        app_names = [x[0] for x in vault_apps] #this displays the stored apps, that the user has entered, if he has any
        app_names_pretty = ''' this is for the join attribute which joins the strings within the variables, making them easier to format
'''.join(app_names)
        print(f'''Here are the apps which you have stored information on,
------------------
{app_names_pretty}
------------------
which apps information do you want to view''') #I have used f-string as it is better than a format statment, as we are printing a list, i can print with no additional brackets and can be manipulated easier to look nuice
        while True: 
            users_passwords = input()
            if users_passwords == "":
                print('''Please enter a valid answer''')
            else:
                for a in vault_apps: #if a (the app information the user is looking for is in thevault_apps list, print it out with the rest of its information like password
                    if users_passwords in a:
                        print('''{}password: {}'''.format(users_passwords, a[1])) 
                        while True: 
                                more_apps = input('''press, 
1) to see the information of your other apps 
2) to return to the main menu 
3) to exit the code 
''')
                                if more_apps == "1": existing_apps 



                                if more_apps == "2": locker_menu_func()


                                if more_apps == "3": exit_code()


                                else:
                                    print('''please input a valid answer''')
                                    break



def store_apps(): 
            while True: 
                        app_name = input('''What is the name of the website/app you 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])
                        addapp()
                        break
def addapp():
                        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 app_summary():
    while True:
        if len(vault_apps) < 1:
            print('''there is no summary of your acount as, 
you currently have 0 apps and passwords stored on your account''')  


        else: print('''You have {} app and its information stored'''.format(len(vault_apps)))
        print('''Your longest password  is charecters''', max(len(a) for a in vault_apps))
        print('''Your shortest password  is charecters''', min(len(a) for a in vault_apps))
        goback_menu()

        break                                          

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 locker \n4) exit password locker 
---------------------------------------------------------------------------------
''')
            print('''----------------------------------------------------------------''')    
            while True:
                        if locker_menu_var == "1": existing_apps()

                        if locker_menu_var == "2": store_apps()

                        if locker_menu_var == "3": app_summary()

                        if locker_menu_var =="4": exit_code()


                        break
print("------------------------------------------------------------------------")
print('''Welcome to password Locker, a place where you can 
store all your passwords to easily enter your precious accounts without
hassle.''')
print("------------------------------------------------------------------------")
print('''First lets make an account,''')

while True:
    first_name = input('''What is your first name? 
''')
    if first_name.isdigit():  #isdigit, detects if there
        print("Please enter a valid answer, No nubers shoud be present")
    elif first_name == "":
        print("Please enter an answer")       

    else:
        break 

while True:
        sur_name = input('''What is your surname?
''')
        if sur_name.isdigit():  #isdigit detects if the
            print("No numbers")
        elif sur_name == "":
            print("Please enter an answer")

        else:
            break
def login_originup1(username, vault_password):
    print(''' Welcome to password vault, 
You can either login or create a New account''')
    while True: 
        login_orsignup1 = input(''' Press \n1) to Log in
:''')
        if login_orsignup1 == "1":                     
            while input('''--------------------------------------------------------
What is your username: ''') != username: 
                print("Incorrect username")

            while input("What is your password: ") == vault_password:
                print('''-----------------------------------------------------------

                ''')


                locker_menu_func()
                break
            else:
                    print("Incorrect password")

#Main Routine       
print('''------------------------------------------------------------------------''')
print('''Welcome, {} {} 
what would you like your username to be, it should be something 
memorable and no longer than fifteen characters long, '''.format(first_name, sur_name))
while True:
        username = input("")
        if 0 < len(username) < 16:
                print('''Nice, username''')
                break
        elif username == "":
            print("Please enter an answer")

        else:
            print('''Your username should be a maximum of 15 charecters, ''')
print('''-------------------------------------------------------------------------''')
while True:
    vault_password = input('''Now it's time to setup a password for your locker, It should be between 4 
and 10 charecters long,  
''')
    if len(vault_password) > 4 and len(vault_password) < 11:
        print('''{}, is locked in thanks for joining Password Locker'''.format(vault_password)) 
        break
    else:
        print("It should be between 4 and 10 charecters long!")
print('''
-------------------------------------------------------------------------------------------''')                    
login_originup1(username, vault_password)

2 个答案:

答案 0 :(得分:1)

您的缩进是错误的。您所有的print调用都应位于else块中。另外,考虑使用f-strings并将生成器表达式的长度分解为单独的list

if not vault_apps:
    print('There is no summary of your account as you have no apps and passwords stored.')  

else: 
    lengths = [len(a) for a in vault_apps]
    print(f'You have {n_apps} apps and their information stored.')
    print(f'Your longest password is {max(lengths)} characters.')
    print(f'Your shortest password is {min(lengths)} characters.')

答案 1 :(得分:1)

$output = preg_replace("/^0/", "254", $input); 块的缩进是错误的,应该是:

else:

通常,您可以避免将else: print('''You have {} app and its information stored'''.format(len(vault_apps))) print('''Your longest password is charecters''', max(len(a) for a in vault_apps)) print('''Your shortest password is charecters''', min(len(a) for a in vault_apps)) 块放在同一行上,这可以避免以后再添加行时出现此类问题。