Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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

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
List 代码>_List_Python 3.x_Dictionary_While Loop_Append - Fatal编程技术网

List 代码>

List 代码>,list,python-3.x,dictionary,while-loop,append,List,Python 3.x,Dictionary,While Loop,Append,到 vault[用户名]=密码 另一方面,我建议不要存储实际密码,而是存储散列。看一看。为什么要使用目录的列表?问题是,您会捕获一个KeyError并继续跳出循环,因为每次尝试检查第二组或第三组登录详细信息时,如果i[username]==password:为什么不保留一个登录详细信息的dict,则在第一次迭代中失败,将该行包装在中,然后尝试“除外”,而不是整个for循环。 vault = [] appvault = [] passvault = [] def menu(): mo

vault[用户名]=密码

另一方面,我建议不要存储实际密码,而是存储散列。看一看。

为什么要使用
目录的
列表?问题是,您会捕获一个
KeyError
并继续跳出循环,因为每次尝试检查第二组或第三组登录详细信息时,如果i[username]==password:
为什么不保留一个登录详细信息的
dict
,则在第一次迭代中失败,将该行包装在
中,然后尝试“除外”,而不是整个for循环。
vault = []
appvault = []
passvault = []

def menu(): 
    mode = input("""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
    > """).strip()
    return mode

def login():
    if len(vault) > 0 : #user has to append usernames and passwords before it asks for login details
        print("Welcome to the login console")
        while True:
            username = input ("Enter Username: ") 
            if username == "":
                print("User Name Not entered, try again!")
                continue
            password = input ("Enter Password: ") 
            if password == "":
                print("Password Not entered, try again!")
                continue
            try:
                for i in vault: 
                    if i[username] == password:
                        print("Username matches!")
                        print("Password matches!")
                        logged() #jumps to logged function and tells the user they are logged on
                        break 
            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!")

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

    while True:
        validname = True
        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():
                print("Your username cannot be null, contain spaces or contain symbols \n")
            elif len(username) < 3:
                print("Your username must be at least 3 characters long \n")
            elif len(username) > 30:
                print("Your username cannot be over 30 characters \n")
            else:
                validname = False 
        validpass = True

        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():
                print("Your password cannot be null, contain spaces or contain symbols \n")
            elif len(password) < 8:
                print("Your password must be at least 8 characters long \n")
            elif len(password) > 20:
                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.append({username:password})
        validinput = True
        while validinput:
            exit = input("\nEnter 'end' to exit or any key to continue to add more username and passwords:\n> ")
            if exit in ["end", "End", "END"]:
                break
            else:
                validinput = False
                register()
        return register

def logged():
    print("----------------------------------------------------------------------\n")
    print("You are logged on.")

while True:
        chosen_option = menu() #a custom variable is created that puts the menu function into the while loop

        if chosen_option in ["a", "A"]:
            login()

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

        else:
            print("""That was not a valid option, please try again:\n """)