如何在python中的字典中具有两个键和一个值 - 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...

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

您如何在列表内部调用一个字符串位置? - python

我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…

Python-crontab模块 - python

我正在尝试在Linux OS(CentOS 7)上使用Python-crontab模块我的配置文件如下:{ "ossConfigurationData": { "work1": [ { "cronInterval": "0 0 0 1 1 ?", "attribute&…

查找字符串中的行数 - python

我正在创建一个python电影播放器​​/制作器,我想在多行字符串中找到行数。我想知道是否有任何内置函数或可以编写代码的函数来做到这一点:x = """ line1 line2 """ getLines(x) python大神给出的解决方案 如果换行符是'\n',则nlines …

字符串文字中的正斜杠表现异常 - python

为什么S1和S2在撇号位置方面表现不同?S1="1/282/03/10" S2="4/107/03/10" R1="".join({"N\'" ,S1,"\'" }) R2="".join({"N\'…