调用返回哈希值的函数:attributeerror:“tuple”对象没有属性“encode”

y0u0uwnf  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(409)

**已关闭。**此问题不可复制或由打字错误引起。它目前不接受答案。
**想要改进此问题?**更新问题,使其位于堆栈溢出主题上。

昨天关门了。
改进这个问题
我有以下代码(中期开发)并且遇到了一个错误。就我所见,我正在向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)

答复https://replit.com/@teachyourselfpython/wiltedlawngreeninvocation#main.py

dsekswqp

dsekswqp1#

这取决于输入sha256(文本)的参数。
文本必须是字符串才能使用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

相关问题