Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x password=input(“请输入要添加到密码保管库的密码。注意:密码长度必须至少为8个字符:”).strip().lower() 如果不是password.isalnum():#处理空输入密码,即包含空格或符号的密码 打印(“您的密码不能为空,不能包_Python 3.x_Loops_While Loop_Integer - Fatal编程技术网

Python 3.x password=input(“请输入要添加到密码保管库的密码。注意:密码长度必须至少为8个字符:”).strip().lower() 如果不是password.isalnum():#处理空输入密码,即包含空格或符号的密码 打印(“您的密码不能为空,不能包

Python 3.x password=input(“请输入要添加到密码保管库的密码。注意:密码长度必须至少为8个字符:”).strip().lower() 如果不是password.isalnum():#处理空输入密码,即包含空格或符号的密码 打印(“您的密码不能为空,不能包,python-3.x,loops,while-loop,integer,Python 3.x,Loops,While Loop,Integer,password=input(“请输入要添加到密码保管库的密码。注意:密码长度必须至少为8个字符:”).strip().lower() 如果不是password.isalnum():#处理空输入密码,即包含空格或符号的密码 打印(“您的密码不能为空,不能包含空格或符号\n”) elif len(密码)20:#长度超过20个字符的密码将被拒绝并返回 打印(“您的密码长度不能超过20个字符\n”) 其他: validpass=False#validpass必须为True才能保留在函数中,否则如果为F

password=input(“请输入要添加到密码保管库的密码。注意:密码长度必须至少为8个字符:”).strip().lower() 如果不是password.isalnum():#处理空输入密码,即包含空格或符号的密码 打印(“您的密码不能为空,不能包含空格或符号\n”) elif len(密码)<8:#长度小于8个字符的密码将被拒绝并返回 打印(“您的密码长度必须至少为8个字符\n”) elif len(密码)>20:#长度超过20个字符的密码将被拒绝并返回 打印(“您的密码长度不能超过20个字符\n”) 其他: validpass=False#validpass必须为True才能保留在函数中,否则如果为False,它将执行另一个操作,在这种情况下,会附加密码。 vault[用户名]=密码#将用户名和密码添加到字典的过程 validinput=True#whileloop询问用户是否要退出或添加更多登录详细信息 有效输入时: exit=input(“\n您想注册更多用户名和密码吗?输入'end'退出,或输入任意键继续添加更多用户名和密码:\n>”) 如果退出[“结束”,“结束”,“结束]:#如果输入符合这些条件,循环将中断,从而跳回主菜单 return#返回被调用函数的输出 其他: validinput=False#如果用户决定不退出并添加更多登录详细信息,则调用register并重复请求登录输入的过程 寄存器() 返回寄存器#将登录详细信息返回给函数 #登录密码和网站应用程序添加控制台------------------------------------------------------------------------------------------------------------------------ def logged():#logged函数是第二个菜单,用户在使用其注册的登录详细信息登录后可以访问该菜单 打印(“-----------------------------------*进入密码保险库菜单*------------------------------------\n”) 打印(“您已登录,欢迎使用密码保险库控制台。”) keeplooping=True#而True的loop将使菜单保持循环 在继续浏览时: 尝试: modea=int(输入(“”)以下是您可以在密码保险库控制台中选择的选项: ##########################################################################\n 1) 查找现有网站/应用程序的密码 2) 添加新网站/应用程序和新密码 3) 密码保险库摘要 4) 出口 ##########################################################################\n > """)) 除值错误外: 打印(“请输入有效选项!”) 如果modea==1:#如果用户按照指示输入1,则他们决定选择查找现有应用程序和密码 viewapp()#调用viewapp函数 elif modea==2:#如果用户按照指示输入2,则他们决定添加新的应用程序/网站及其密码 addapp()#调用addapp函数 elif modea==3:#如果用户按照指示输入3,则他们决定查看迄今为止输入的密码摘要 summary()#调用summary函数 elif modea==4:#如果用户按照指示输入4,则他们已决定退出整个程序。所有循环停止,程序以打印语句“再见”结束 keeploping=False 打印(“*正在退出程序*\n”) 时间。睡眠(2) 3月月日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日########################### 打印(“再见用户,感谢使用密码保险库程序”) 3月月日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日########################### 回程暂停 其他: 打印(“该选项无效,请重试:”) validintro=False def viewapp():#viewapp功能是密码和vault菜单的第一个选项,允许用户查看迄今为止存储的应用/网站和密码 如果len(appvault)>0:#只有在用户输入一组信息后才会显示网站/应用程序和密码,否则不会显示任何信息 打印(“”)以下是迄今为止存储的网站/应用程序和密码的详细信息: (注意:网站/应用程序和密码按附加顺序显示;第一个密码用于第一个网站等…。\n“”) 对于appvault中的应用程序: 打印(“这是您存储的网站/应用:”) 打印(“-{}\n”。格式(应用程序))#网站/应用程序在此处打印 如果len(passvault)>0:#只有在用户输入一组信息后才会显示网站/应用程序和密码,否则不会显示任何信息 对于passvault中的代码: 打印(“以下是您为网站/应用程序存储的密码:”) 打印(“-{}\n”.format(code))#密码在这里打印出来 其他: 打印(“您尚未输入任何应用程序或密码!”) def addapp(): 尽管如此: validapp=True 而validapp: 新建应用程序=输入(“输入新网站/应用程序名称:”).strip().lower() 如果len(new_app)>20:#如果用户进入一个超过20个字符的网站,该网站将被拒绝并循环返回并再次请求输入 打印(“请输入不超过20个字符的新网站/应用程序名称:”) elif len(新应用程序)<1:#如果用户未输入任何内容,程序将返回并再次请求输入 打印(“请输入有效的新网站/应用程序名称:”) 其他: validapp=False appvault.append(新的应用程序) validnewpass=True 而瓦尔
#import modules
import time
import sys
import re

#Initialise variables that could help with error handling
name = ""
mode = ""
username = ""
password = ""
addusername = ""
addpassword = ""
exit = ""
modea = ""
new_app = ""
new_pass = ""
quit = ""

#Dictionary to store more than one set of login details
vault = {}

#Lists to store website names and passwords
appvault = []
passvault = []


#FIRST MENU AND REGISTERING LOGIN DETAILS -----------------------------------------------------------------------------------------------------------------------------------------

def menu(): #The menu function is the 1st menu that asks the user to either register their login details or to login with existing login details saved into the vault dictionary
    print("--------------------------*ENTERING THE MAIN MENU*-----------------------------------\n") #triple """ used to effectively format the menu
    mode = input(str("""Hello {}, below are the modes that you can choose from:\n 
    ##########################################################################
    a) Login with username and password
    b) Register as a new user
    To select a mode, enter the corresponding letter of the mode below
    ##########################################################################\n
    > """.format(name))).strip() #greets user and removes any white space
    return mode #returns the input

def login(username, password): #The login function is where the user logins in with existing registered details in the vault dictionary
    if len(vault) > 0 : #user has to append usernames and passwords before it asks for login details
        print("Welcome {} to the login console".format(name))
        noloop = True #Starts a loop if True will begin looping
        while noloop:
            addusername = input("Please enter username: ") 
            if addusername == "":
                print("Username not entered, try again!")
                continue #returns to the beginning of the while loop if null
            addpassword = input("Please enter password: ") 
            if addpassword == "":
                print("Password not entered, try again!")
                continue #returns to the beginning of the while loop if null
            try:
                if vault[addusername] == addpassword: #If the username and password match with the login details appended into the dictionary, it will count as matching details.
                    print("Username matches!")
                    print("Password matches!")
                    print("Logging into password vault...\n")
                    noloop = logged() #jumps to logged function and tells the user they are logged on
                    return noloop #to return the noloop depending on users option (True or False)
            except KeyError: #the except keyerror recognises the existence of the username and password in the list
                print("The entered username or password is not found!")

    else:
        print("You have no usernames and passwords stored!") #When the user selects option A before adding any login details to the dictionary
        return True #returning to the start of the loop when the else statement is printed

def register(): #example where the username is appended. Same applies for the password
    global username #initialises global variables
    global password
    print("Please create a username and password into the password vault.\n")

    while True:
        validname = True #whileloop will loop the username variable and while True it runs the loop
        while validname:
            username = input("Please enter a username you would like to add to the password vault. NOTE: Your username must be at least 3 characters long: ").strip().lower()
            if not username.isalnum(): #handles usernames with no input, contain spaces between them or have symbols in them.
                print("Your username cannot be null, contain spaces or contain symbols \n")
            elif len(username) < 3: #any username with less than 3 characters is rejected and looped back
                print("Your username must be at least 3 characters long \n")
            elif len(username) > 30: #any username with more than 30 characters is rejected and looped back
                print("Your username cannot be over 30 characters long \n")
            else:
                validname = False #whileloop is False will mean the loop will break and proceed onto asking password
        validpass = True #whileloop will loop the password variable and while True it runs the loop

        while validpass:
            password = input("Please enter a password you would like to add to the password vault. NOTE: Your password must be at least 8 characters long: ").strip().lower()
            if not password.isalnum(): #handles null input passwords, passwords that contain spaces or symbols
                print("Your password cannot be null, contain spaces or contain symbols \n")
            elif len(password) < 8: #passwords that are less than 8 characters long are rejected and loops back
                print("Your password must be at least 8 characters long \n")
            elif len(password) > 20: #passwords that are more than 20 characters long are rejected and loops back
                print("Your password cannot be over 20 characters long \n")
            else:
                validpass = False #The validpass has to be True to stay in the function, otherwise if it is false, it will execute another action, in this case the password is appended.
        vault[username] = password #the appending process of username and passwords to the dictionary
        validinput = True #whileloop asking for the user if they want to quit or add more login details
        while validinput:
            exit = input("\nDo you want to register more usernames and passwords? Enter 'end' to exit or any key to continue to add more username and passwords:\n> ")
            if exit in ["end", "End", "END"]: #if input matches these conditions, the loop will then break and thus jump back to the main menu
                return #returns outputs to the called function
            else:
                validinput = False #if user decides not to quit and to add more login details, then register is called and the process of asking for login input repeats
                register()
        return register #returns login details to the function


#LOGGED ONTO THE PASSWORD AND WEBSITE APP ADDING CONSOLE------------------------------------------------------------------------------------------------------------------------

def logged(): #The logged function is the 2nd menu where the user is able to access after logging in with their registered login details
    print("-----------------------------*ENTERING THE PASSWORD VAULT MENU*-----------------------------------\n")
    print("You are logged in, welcome to the password vault console.")
    keeplooping = True #whileloop that is True will keep the menu in a loop
    while keeplooping:    
        try:
            modea = int(input("""Below are the options you can choose from in the password vault console:
            ##########################################################################\n
            1) Find the password for an existing website/app
            2) Add a new website/app and a new password for it
            3) Summary of the password vault
            4) Exit
            ##########################################################################\n
            > """))
        except ValueError:
            print("Please enter a valid option!")

    if modea == 1: #if the user enters 1 as instructed, then they have decided to choose to find existing apps and passwords
        viewapp() #viewapp function is called

    elif modea == 2: #if user enters 2 as instructed, then they have decided to add new app/websites and the passwords for it
        addapp() #addapp function is called

    elif modea == 3: #if the user enters 3 as instructed, then they have decided to look at the summary of passwords entered so far
        summary() #summary function is called

    elif modea == 4: #if the user enters 4 as instructed, then they have deicided to quit the entire program. All loops stop and program ends with print statement saying "Goodbye"
        keeplooping = False 
        print("*Exiting program*\n")
        time.sleep(2)
        print("############################################################")
        print("Goodbye user and thanks for using the password vault program")
        print("############################################################")
        return keeplooping 
    else:
        print("That was not a valid option, please try again: ") 
        validintro = False 


def viewapp(): #The viewapp function is the 1st option of the password and vault menu that allows the user to view the app/websites and passwords stored so far
    if len(appvault) > 0: #The website/apps and passwords are only shown once the user has entered a set of the info, otherwise no info will be shown
        print("""Below are the details of the website/apps and passwords stored so far: 
(NOTE: Websites/apps and passwords are shown in order of being appended; 1st password is for 1st website, etc...\n""")
        for app in appvault: 
            print("Here are the website/app you have stored:")
            print("- {}\n".format(app)) #Website/app is printed out here
    if len(passvault) > 0 : #The website/apps and passwords are only shown once the user has entered a set of the info, otherwise no info will be shown
        for code in passvault: 
            print("Here are the password you have stored for the websites/apps: ")
            print("- {}\n".format(code)) #The passwords are printed out here

    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: #if the user enters a website with more than 20 characters, it is rejected and loops back and asks for input again
                print("Please enter a new website/app name with no more than 20 characters: ")
            elif len(new_app) < 1: #if the user enters nothing, program loops back and asks for input again
                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: ").strip()
            if not new_pass.isalnum(): #checks if the entered username has spaces, or symbols or is a null input, which would be rejected and the program will loop back
                print("Your password for the website/app cannot be null, contain spaces or contain symbols \n")            
            elif len(new_pass) < 8: #the password must be at least 8 characters long to be accepted as valid
                print("Your new password must be at least 8 characters long: ")
            elif len(new_pass) > 20: #the password must not exceed 20 characters, otherwise it would be rejected and will loop back
                print("Your new password cannot be over 20 characters long: ")   
            else:
                validnewpass = False 
                passvault.append(new_pass) 

        validquit = True 
        while validquit:
            quit = input("\nDo you want to enter more websites/apps and passwords? Enter '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"]: 
                return
            else:
                validquit = False 
                addapp() 
            return addapp        

def summary(): #The summary function is a print statement of how many password have been stored, the passwords with the longest or shortest characters are also displayed
    if len(passvault) > 0: #The user must have entered at least 1 password before the summary option can be viewed
            print("----------------------------------------------------------------------")
            print("Here is a summary of the passwords stored in the password vault:\n")
            print("The number of passwords stored so far:", len(passvault)) #len counts the amount of passwords stored in the passvault list
            print("Passwords with the longest characters: ", max(new_pass for (new_pass) in passvault)) #max finds the passwords appended in the passvault list with the longest password
            print("Passwords with the shortest charactrs: ", min(new_pass for (new_pass) in passvault)) #min finds the passwords appended in the passvault list with the shortet password
            print("----------------------------------------------------------------------")
    else:
        print("You have no passwords entered yet!")     

#Main routine
print("Welcome user to the password vault program")
print("In this program you will be able to store your usernames and passwords in password vaults and view them later on.\n")
validintro = False 
while not validintro:
    name = input(str("Greetings user, what is your name?: ")).lower().strip()
    if not re.match("^[a-z]*$", name):
        print("Your name must only contain letters from a-z: ")
    elif len(name) < 1: 
        print("Please enter a name that is valid: ")
    elif len(name) > 30:
        print("Please enter a name with no more than 30 characters long: ")
    else:
        validintro = True 
        print("Welcome to the password vault program {}.\n".format(name))

#The main program to run in a while loop for the program to keep on going back to the menu part of the program for more input till the user wants the program to stop
validintro = False 
while not validintro: 
        chosen_option = menu() 
        validintro = False

        if chosen_option in ["a", "A"]: 
            validintro = not login(username, password)

        elif chosen_option in ["b", "B"]: 
            register() 

        else:
            print("""That was not a valid option, please try again:\n """) 
            validintro = False 
except ValueError:
        print("Please enter a valid option!")
        continue