Calling a function that returns a hash value: AttributeError: 'tuple' object has no attribute 'encode'
Calling a function that returns a hash value: AttributeError: 'tuple' object has no attribute 'encode'
我有以下代码(开发中期)并且 运行 出错了。据我所知,我正在将文本传递给 SHA256 函数,但它表明它是一个元组。
from hashlib import sha256
MAX_NONCE=10000
def SHA256(text):
return sha256(text.encode("ascii")).hexdigest() #returns a hash value
def mine(blockno,transactions,previous_hash,prefix):
nonce=1
text=str(blockno)+transactions+previous_hash+str(nonce)
new_hash=SHA256(text)
print(new_hash)
#STARTING POINT OF THE PROGRAM
transaction ="""
STUFF HERE
"""
difficulty=3
newhash = mine(7,transaction,"F823482798723897293874982734982F",difficulty)
错误
Traceback (most recent call last):
File "main.py", line 24, in <module>
newhash = mine(7,transaction,"F823482798723897293874982734982F",difficulty)
File "main.py", line 11, in mine
new_hash=SHA256(text)
File "main.py", line 5, in SHA256
return sha256(text.encode("ascii")).hexdigest() #returns a hash value
UnicodeEncodeError: 'ascii' codec can't encode character '\xa3' in position 29: ordinal not in range(128)
repl.it
https://replit.com/@teachyourselfpython/WiltedLawngreenInvocation#main.py
这取决于您输入 SHA256(text) 的参数。
文本必须是字符串才能使 encode() 起作用。
您也可以尝试处理元组。
def SHA256(text):
if type(text) == str:
return sha256(text.encode("ascii")).hexdigest() #returns a hash value
else:
list_of_hashes = []
for each in text:
list_of_hashes.append(sha256(text.encode("ascii")).hexdigest())
return list_of_hashes
我有以下代码(开发中期)并且 运行 出错了。据我所知,我正在将文本传递给 SHA256 函数,但它表明它是一个元组。
from hashlib import sha256
MAX_NONCE=10000
def SHA256(text):
return sha256(text.encode("ascii")).hexdigest() #returns a hash value
def mine(blockno,transactions,previous_hash,prefix):
nonce=1
text=str(blockno)+transactions+previous_hash+str(nonce)
new_hash=SHA256(text)
print(new_hash)
#STARTING POINT OF THE PROGRAM
transaction ="""
STUFF HERE
"""
difficulty=3
newhash = mine(7,transaction,"F823482798723897293874982734982F",difficulty)
错误
Traceback (most recent call last):
File "main.py", line 24, in <module>
newhash = mine(7,transaction,"F823482798723897293874982734982F",difficulty)
File "main.py", line 11, in mine
new_hash=SHA256(text)
File "main.py", line 5, in SHA256
return sha256(text.encode("ascii")).hexdigest() #returns a hash value
UnicodeEncodeError: 'ascii' codec can't encode character '\xa3' in position 29: ordinal not in range(128)
repl.it https://replit.com/@teachyourselfpython/WiltedLawngreenInvocation#main.py
这取决于您输入 SHA256(text) 的参数。
文本必须是字符串才能使 encode() 起作用。
您也可以尝试处理元组。
def SHA256(text):
if type(text) == str:
return sha256(text.encode("ascii")).hexdigest() #returns a hash value
else:
list_of_hashes = []
for each in text:
list_of_hashes.append(sha256(text.encode("ascii")).hexdigest())
return list_of_hashes