我是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 感谢您的任何提前帮助
答案 0 :(得分:0)
字典的想法是将值与用于查找它的键相关联,就像在真正的字典中如何查找单词(键)以查找其定义(值)一样。
出现错误是因为您试图将整个词典本身用作在其中查找内容的键;这没有任何意义,因此不起作用。您可能想做更多类似的事情:
new_user["password"] = hashed_password
它的作用并不明显,但是如果您要做的只是“将字符串存储在字典中”,那么就符合条件。