如何在python中的字典中具有两个键和一个值

约翰努

我正在制作一个使用简单字典存储帐户名和密码的小型登录系统。我想知道是否有可能使用电子邮件和用户名,然后使用对两者都有效的密码。例如,我唯一的帐户是密码为“ h1”的“ hello”,我如何才能使密码为“ h1”的“ [email protected]”或“ hello”解锁?

accounts = {'hello':'h1'}
print(accounts)
started = ""
while started != "q":
    started = input("Are you a registered user y/n: (if you want to quit press any key)")  

if started == "n": 
     createLogin = input("Create your desired login name: ")

     if createLogin in accounts: 
         print ("Login name already exist!")
     else:
         createPassw = input("Create your desired password password: ")
         accounts[createLogin] = createPassw
         print("User created!")
         addemail = input("What is your email address :")
         accounts[createLogin].append(addemail)             

elif started == "y": 
    login = input("Enter your login name: ")

    if login in accounts:
        passw = input("Enter your password")

    else:
        print("user doesnt exist.")




    if login in accounts and accounts[login] == passw:
        print("Login successful!")
        changepassword = input("Would you like to change your password?")
        if changepassword in ["Y", "y"]:
            newpassw = input("What do you want your new password to be?")
            accounts[login] = newpassw
            print("password succesfully changed!")
        else:
            print("Password was not changed.")
    else:
        print("Incorrect password!")

我试图将电子邮件添加到用户名,以便字典看起来像{['hello @ mail.com,'hello']:'h1'}。有任何想法吗?

弥撒

你是说这里

     accounts[createLogin].append(addemail)             

那当然是行不通的,因为值是string,而string没有.append同样,将电子邮件附加到密码上也不会很紧张。

我认为这不是一个好的结构: {['[email protected],'hello']:'h1'}

我会accounts列出一些字典:

accounts = [
    {
        'email': '[email protected]', 
        'username': 'hello', 
        'password': 'h1',
    },
]

然后,您可以查看是否有具有给定用户名或电子邮件的帐户(我将使用function,希望可以):

def find_account_by_login(accounts, login):
    for account in accounts:
        if account['email'] == login or account['username'] == login:
            return account
    # no account matched the login
    return None
elif started == "y": 
    login = input("Enter your login name: ")
    account = find_account_by_login(accounts, login)
    if not account:
        print("user doesnt exist.")
    else:
        passw = input("Enter your password")
        if passw != account['password']:
            print("Incorrect password!")
        else:
            print("Login successful!")
            # change the password...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在python中具有相同键的两个不同字典中组合值

Python-如何使用具有一个键和两个值的循环更新字典?

如何从键有两个值的字典中输出一个值

如何在python中合并两个具有相同键和数组类型值的嵌套字典?

当一个键在 python 中应该有两个值时,创建一个包含两个列表的字典

python-在具有两个条件的另一个文件中查找字典键

当两个键的值相同时,从python字典中删除一个项目

如何从具有两个键的变量中删除一个键?

从两列中创建一个具有唯一键和值列表的字典

如何从具有两个键值对的字典列表中基于另一个kv对的值创建一个键值对中的值列表

如何将两个列表中具有相同键的字典值相加?

Python - 如何在 django 模板中并排显示所有值的两个键的字典值

如何在字典中存储两个输出,一个作为键,另一个作为值

在字典中为一个键添加具有多个值的列表或字典-Python

python字典具有一个键的多个属性。如何在这些属性之一中找到最小值并返回键?

Python:具有一个键和多个值的字典:如何获取具有相同SET值的键列表?

如果两个键都位于两个分开的字典中,如何获取字典键和值?

如何用两个键和一个值创建字典?

使用 zip() 将两个列表组合成一个字典,在 appand multipal 值中具有相同的键

如何从每个键具有多个值的字典中写入一个csv文件?

在 Python 中的第一个键上具有默认值的嵌套字典

如何从两个单独的数组中创建具有两个键/值对的对象

具有两个键和多个值的文件中的Python-Dictionary

如何在一个ggplot中具有两个不同大小的图例?

C#-如何用一个键和两个值编写一个集合(字典?)

Python 3.x:合并两个具有相同键和值的字典

在具有相同键的字典中合并两个Python字典

如何在字典中只提取一个值和键?

在两个列表中合并具有相同特定键和值的字典的最快方法是什么?