【问题标题】:I would like to use a dictionary to store new user names and password我想使用字典来存储新的用户名和密码
【发布时间】:2020-10-27 02:07:09
【问题描述】:

如何使此代码将新用户名和密码存储在用户名为 (K) 和密码为 (V) 的字典中?或者我是否需要将它们存储在列表中,如果需要,我该怎么做?我已经编写了另一段代码用于返回用户的登录,它访问字典中的 (K) 和 (V) 的用户名和密码。

while True:
    new_user = input("Consider using alphanumeric and special characters \nCreate new user ID: ")
    if len(new_user) < 4:
        print("User ID must be more than 4 alphanumeric characters".upper())
        continue
    elif len(new_user) > 10:
        print("User ID must be less than 10 alphanumeric characters".upper()) 
        continue  
    else:
        print("Please confirm user ID ")
        break

【问题讨论】:

  • di[user_id]=password就这么简单
  • 这取决于您要如何获取它们。直接存储密码不是最佳做法。
  • @RishabhDeepSingh 我写了另一段代码来引用字典。感谢您的回复。存储密码的最佳做法是什么?

标签: python dictionary


【解决方案1】:

您可以使用mydict={} 创建一个新字典,然后使用mydict["your-key"] = "your-value" 存储键及其值

你可以找到更多here,记住保存textplain密码真的很危险,尤其是这样!

另外,正如user8408080所说:

您通常会立即散列密码并保存该散列。 当用户尝试使用他的密码登录时,这也将 被散列并且散列将被比较。这样没人能偷 数据库中的密码

【讨论】:

  • 补充:如果 OP 想知道,还能做什么:您通常会立即散列密码并保护该散列。当用户尝试使用他的密码登录时,它也将被散列并且将比较散列。这样就没有人可以从数据库中窃取您的密码
  • 好的,谢谢!我可以将它添加到答案并标记你吗?
  • 没问题,这就是 cmets 的用途:D
【解决方案2】:

为了进一步构建您提供的代码,这里有一个小示例。当然,您首先必须创建您的字典。我称它为users_and_passwords。请记住,完全不鼓励将用户和密码直接存储在字典中!

users_and_passwords = dict()

# get-user loop
while True:

    new_user = input("Consider using alphanumeric and special characters \nCreate new user ID: ")
    if len(new_user) < 4:
        print("User ID must be more than 4 alphanumeric characters".upper())
        continue
    elif len(new_user) > 10:
        print("User ID must be less than 10 alphanumeric characters".upper()) 
        continue  
    else:
        print("Please confirm user ID ")
        break


# get-password loop
while True:
    
    user_password = input("Create new user password: ")

    # do whatever logic here
    # let's assume its fine now
    break

正确填写用户和密码变量后,可以将它们作为键:值对添加到字典中,如下所示:

users_and_passwords[new_user] = user_password

然后您可以像这样遍历您的用户及其密码:

for k, v in users_and_passwords.items():
    print (f'user: {k}\thas password: {v}')

为了证明这是可行的,在本例中使用user123 作为用户名,pass123 作为密码,输出将是:

user:user123    has password: pass123

注意直接存储密码!

正如其他人已经指出的那样,简单地不可逆地加扰您的密码(例如,通过散列它,您可以查看this)并将加扰的密码用作您的用户的值会使这更安全。

【讨论】:

    【解决方案3】:
    users = [{'K':'john','V':'123456'}]
    
    while True:
      username = input("Consider using alphanumeric and special characters \nCreate new user ID: ")
      if len(username) < 4:
        print("User ID must be more than 4 alphanumeric characters".upper())
        continue
      elif len(username) > 10:
        print("User ID must be less than 10 alphanumeric characters".upper()) 
        continue  
      else:
        # getting the password
        password = input('Password: ')
        # creating a new user with a dict
        new_user = {'K':username,'V':password}
        # adding the new user to dict
        users.append(new_user)
    
        # I added this for testing purposes, delete these 3 lines in real use
        if(len(users) > 1):
          break
        continue
        
        # and uncomment the next line
        # break
    
    def login(username, password):
      for user in users:
        # checking if the given username and password matches a user in the users list
        if(user['K'] == username and user['V'] == password):
          print('Logged in')
          return True
      
      return False
    
    
    print('\n\n\n')
    print(users)
    print('\n\n\n')
    print('Log in')
    username = input('Username: ')
    password = input('Password: ')
    
    login(username,password)
    

    我将用户添加到列表中,并在登录时通过列表搜索并检查用户名和密码是否匹配。

    注意:在生产中永远不要以纯文本形式保存密码,使用 sha256 之类的东西对其进行哈希处理

    【讨论】:

    • 为什么要使用字典列表而不是字典?这是两全其美的结果......
    • 我只是在这里创建了一个示例,在这些情况下,应该使用数据库来提高效率。而且我不想将每个用户名都存储在字典中,实际上如果我喜欢这样会更好,sry
    猜你喜欢
    • 2019-04-07
    • 2013-12-27
    • 1970-01-01
    • 2010-12-27
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    相关资源
    最近更新 更多