python - 如何在Python中的字典中拥有两个键和一个值

标签 python python-3.x

我正在制作一个小型登录系统,它使用一个简单的字典来存储帐户名和密码。我想知道是否可以拥有电子邮件和用户名,然后拥有适用于两者的密码。 例如,我唯一的帐户是“hello”,密码为“h1”,我如何才能使“hello@mail.com”或“hello”以密码“h1”解锁?

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)             

这当然不起作用,因为该值是字符串,而字符串没有.append。此外,将电子邮件附加到密码也不会造成紧张。

我认为这不是一个好的结构:{['hello@mail.com,'hello']:'h1'}

我会让accounts成为一个字典列表:

accounts = [
    {
        'email': 'hello@mail.com', 
        'username': 'hello', 
        'password': 'h1',
    },
]

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

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...

关于python - 如何在Python中的字典中拥有两个键和一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54165732/

相关文章:

三维数组的Python列表理解

python - 在类对象中存储未绑定(bind)的 python 函数

python - Spark 和 Python : strategy for parallelize/map statsmodels sarimax

python - 类型错误 : does not support the buffer interface Twitch IRC Chat Bot

javascript - Bokeh 中的自定义 javascript 回调可从数据框中选择列并更新绘图

python - 为什么当通过 swig 从子进程调用时,使用 OpenMP 的共享库中的函数会挂起?

python - 如何使用 SQLAlchemy + PostgreSQL 删除自定义类型?

python - 将文件转换为 Ascii 会引发异常

python - 绘制不同频率的时间数据(matplotlib、pandas)

python - 如何从 python 轮中排除 *.pyc 和 __pycache__?