如何在字典中存储散列字符串

How to Store a Hashed String in a Dictionary

我是 python 的新手,我正在尝试将散列字符串存储在字典中。我不知道如何使用谷歌搜索也没有运气,有人可以帮助我吗?这是我的代码:

'''

  import hashlib

  has_account = input('Do you have an account already (Y or N)?: ')
  has_account = str.title(has_account)


  if has_account == 'N':
 new_user = {}
 new_username = input('Please create a username: ')
 new_password = input('Please enter a password at least 6 digits long: ')
 while len(new_password) < 6:
     new_password = input('Please enter a password at least 6 digits long: ')
  reentered_password = input('Please reenter you password: ')
  while new_password != reentered_password:
     print('Passwords are different, please renter both passwords')
      new_password = input('Please enter a password at least 6 digits long: ')
      while len(new_password) < 6:
         new_password = input('Please enter a password at least 6 digits long: ')
      reentered_password = input('Please reenter you password: ')

   buffer = new_password.encode('utf-8')
  hash_object = hashlib.sha1(buffer)
  buffer = hash_object.hexdigest()
  hashed_password = buffer
  del new_password, buffer, reentered_password
  new_user[new_user] = hashed_password
  del new_user, hashed_password

'''

它只是输出:“new_user[new_user] = hashed_password TypeError:无法散列的类型:'dict'” 粘贴在这里可能会弄乱间距,这是原始代码的屏幕截图: enter image description here 提前感谢您的帮助

字典的概念是将值与用于查找它的键相关联,就像在真实字典中查找单词(键)以找到其定义(值).

你犯了这个错误,因为你试图使用整个字典本身作为查找内容的关键;这是没有意义的,因此是行不通的。你可能想做更多类似的事情:

new_user["password"] = hashed_password

这有什么用尚不明显,但如果您要做的只是 "store a string in a dictionary" 那么这就够用了。