如何在多个字典列表中进行迭代

时间:2020-03-31 10:03:08

标签: python-3.x

我正在编写一个简单的代码来询问用户名和密码,并将这些信息匹配到数据库中。如果用户名不在数据库中,我会询问用户是否要注册,然后将该信息添加到数据库中。

代码中的所有内容都运行良好。除了当我在数据库中添加另一个用户时。

在此代码中,如果用户输入Bob和密码test123,则会在数据库中识别出该用户,并退出代码。但是,如果用户输入Rolf,则无法识别,并要求他注册。

如何调整代码以遍历整个数据库。

代码


database = [{"username": 'Bob', "Password": 'test123'},{"username": 'Rolf', "Password": "password2"}]

def askuser():
input_username = input("Please enter your username: ")
input_password = input("Please enter your password: ")
for user in database:
    if user["username"] == input_username and user["Password"] == input_password:
        print(f"username {input_username} is correct, welcome to the program")
        break
    else:
        choice = input(f"username {input_username} doesnt exist, do you want to sign up Y/n : ")
        choice.strip('')
        if choice == "Y":
            while True:
                    input_desireduser = input("Please enter your desired username: ")
                    input_desiredpass = input("Please enter your desired password: ")
                    input_desiredpassconf = input("Please enter your desired password again: ")
                    if (input_desiredpassconf != input_desiredpass):
                        print("Your passwords doesn't match, please enter them again !!")
                        continue
                    else:
                        print("Thank you, your account is created")
                        database.append({"username": input_desireduser, "Password": input_desiredpass})
                        break
        else:
            print("Thank you !!")
            break

return database

askuser()

1 个答案:

答案 0 :(得分:0)

即使存在多个问题,问题也来自if / else。 假设我输入Rudolph和正确的密码。 第一循环: 如果Bob == Rudolph为False,则它将执行else部分。

您将不得不遍历列表,并使用mybool = mybool或我的条件来收集布尔值。 然后再检查。

使用词典列表是否是强制性的? 在此示例中,这没有任何意义。 这是一个没有字典的列表

database = {'bob':'test123','Rudolph':'password'}
 defAskUser()
    input_username = input("Please enter your username: ")
    input_password = input("Please enter your password: ")
    try:
        password = dataset[input_username]
        if password == input_password:
            print(f"username {input_username} is correct, welcome to the program")
    except KeyError:
        choice = input(f"username {input_username} doesnt exist, do you want to sign up Y/n : ")
        choice.strip('')
        if choice == "Y":
                    input_desireduser = input("Please enter your desired username: ")
                    input_desiredpass = input("Please enter your desired password: ")
                    input_desiredpassconf = input("Please enter your desired password again: ")
                    if (input_desiredpassconf != input_desiredpass):
                        print("Your passwords doesn't match, please enter them again !!")
                        continue
                    else:
                        print("Thank you, your account is created")
                        database.append({input_desireduser:input_desiredpass})
                        break
        else:
            print("Thank you !!")
            break