【问题标题】:IndentationError: expected an indented block cannot loop script [duplicate]IndentationError:预期缩进的块不能循环脚本[重复]
【发布时间】:2020-09-13 09:07:37
【问题描述】:

我正在尝试制作一个循环,以便脚本不断生成哈希

from bitcoin import *
import os
import hashlib
import base58

while True:
    priv =  random_key()
    pubkey = privtopub(priv)
    compress_pubkey = False
    
 
    if (compress_pubkey):
    if (ord(pubkey[-2:].decode('hex')) % 2 == 0):
        pubkey_compressed = '02'
    else:
        pubkey_compressed = '03'
    pubkey_compressed += pubkey[2:66]
    hex_str = bytearray.fromhex(pubkey_compressed)
    else:
        hex_str = bytearray.fromhex(pubkey)
 
key_hash = hash160(hex_str)

我正在使用 Python 3,但出现错误:

    if (ord(pubkey[-2:].decode('hex')) % 2 == 0):
     ^
IndentationError: expected an indented block

【问题讨论】:

  • TL:DR 选择此 if 和下面的行,然后按键盘上的 TAB。 Python 需要缩进 if 块内的代码

标签: python python-3.x random


【解决方案1】:

您收到错误是因为第一个 if 语句中没有任何内容,您可以使用 pass 关键字来删除错误或删除 if 语句,如果您不需要它(参见代码中的 cmets)

from bitcoin import *
import os
import hashlib
import base58

while True:
    priv =  random_key()
    pubkey = privtopub(priv)
    compress_pubkey = False
    
 
    if (compress_pubkey): #error
        pass #add something hereor add pass keyword to remove error
    if (ord(pubkey[-2:].decode('hex')) % 2 == 0):
        pubkey_compressed = '02'
    else:
        pubkey_compressed = '03'
        hex_str = bytearray.fromhex(pubkey) # you can't have more than one else
    pubkey_compressed += pubkey[2:66]
    hex_str = bytearray.fromhex(pubkey_compressed)
 
key_hash = hash160(hex_str)

【讨论】:

  • 现在我收到错误:否则:^ SyntaxError: invalid syntax
  • 如果仔细查看解决方案,你有 2 个 else 语句我已将你的两个 else 合二为一,因为这是语法错误。
  • 我收到错误:AttributeError: 'str' object has no attribute 'decode'
  • 写这不是真的:if (ord (pubkey [-2:].Decode ('hex'))% 2 == 0):
猜你喜欢
  • 2021-07-12
  • 1970-01-01
  • 2018-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-15
  • 1970-01-01
相关资源
最近更新 更多