Python 上的语法错误 运行
Syntax Error running on Python
我正在尝试遵循 JS 中的区块链教程,但我正在 Python 上尝试。
我已经走到这一步了,当我试图测试 运行 它时,我得到了这个语法错误,这让我很困惑,因为它看起来是合法的。
有什么想法吗?
import hashlib
class block:
def __init__(self, index, timestamp, data, previous= ''):
self.index = index
self.timestamp = timestamp
self.data= data
self.previous = previous
self.hash = ''
def calculateHash(self):
return hashlib.sha256(self.index + self.previous + self.timestamp+ (self.data).__str__()
class blockchain:
#btw this it where it says the error is: "class"
def __init__(self):
self.chain= [self.createGenesisBlock()]
def createGenesisBlock(self):
return block(0, "01/01/2017", "Genesis Block", "0")
def getLatestBlock(self):
return self.chain[len(self.chain)-1]
def addBlock(self, newBlock):
newBlock.previous = self.getLatestBlock().hash
newBlock.hash= newBlock.calculateHash()
self.chain.push(newBlock)
korCoin = blockchain()
korCoin.addBlock(block(1, "10/07/2017", 4))
korCoin.addBlock(block(2, "12/07/2017", 40))
if __name__ = "__main__":
print(korCoin)
你的class中的函数都有错误的缩进。试试这个 -
import hashlib
class Block:
def __init__(self, index, timestamp, data, previous= ''):
self.index = index
self.timestamp = timestamp
self.data= data
self.previous = previous
self.hash = ''
def calculateHash(self):
return hashlib.sha256(self.index + self.previous + self.timestamp+ (self.data).__str__()
class BlockChain:
def __init__(self):
self.chain= [self.createGenesisBlock()]
def createGenesisBlock(self):
return block(0, "01/01/2017", "Genesis Block", "0")
def getLatestBlock(self):
return self.chain[len(self.chain)-1]
def addBlock(self, newBlock):
newBlock.previous = self.getLatestBlock().hash
newBlock.hash= newBlock.calculateHash()
self.chain.push(newBlock)
korCoin = BlockChain()
korCoin.addBlock(block(1, "10/07/2017", 4))
korCoin.addBlock(block(2, "12/07/2017", 40))
if __name__ == "__main__":
print(korCoin)
遵循python的缩进here
您缺少右括号:
hashlib.sha256(self.index + self.previous + self.timestamp+ (self.data).__str__()
hashlib.sha256(self.index + self.previous + self.timestamp+ (self.data).__str__())
我假设您已经正确缩进了您的实际代码,但在 OP 中粘贴了错误的标识。这就是第一个答案要求您修复缩进的原因。
代码还有其他问题
self.index
是一个整数,你必须在与其他人连接之前将它转换为字符串。您不能连接字符串和整数
self.previous
是一个空字符串('') in the first
korCoin.addBlock` 调用但在第二次调用中是一个 Hash 对象。您必须将其转换为字符串或在与其他人连接之前检索其字符串表示
self.chain
是一个列表,列表没有 push
方法。我想你的意思是 append
我正在尝试遵循 JS 中的区块链教程,但我正在 Python 上尝试。
我已经走到这一步了,当我试图测试 运行 它时,我得到了这个语法错误,这让我很困惑,因为它看起来是合法的。
有什么想法吗?
import hashlib
class block:
def __init__(self, index, timestamp, data, previous= ''):
self.index = index
self.timestamp = timestamp
self.data= data
self.previous = previous
self.hash = ''
def calculateHash(self):
return hashlib.sha256(self.index + self.previous + self.timestamp+ (self.data).__str__()
class blockchain:
#btw this it where it says the error is: "class"
def __init__(self):
self.chain= [self.createGenesisBlock()]
def createGenesisBlock(self):
return block(0, "01/01/2017", "Genesis Block", "0")
def getLatestBlock(self):
return self.chain[len(self.chain)-1]
def addBlock(self, newBlock):
newBlock.previous = self.getLatestBlock().hash
newBlock.hash= newBlock.calculateHash()
self.chain.push(newBlock)
korCoin = blockchain()
korCoin.addBlock(block(1, "10/07/2017", 4))
korCoin.addBlock(block(2, "12/07/2017", 40))
if __name__ = "__main__":
print(korCoin)
你的class中的函数都有错误的缩进。试试这个 -
import hashlib
class Block:
def __init__(self, index, timestamp, data, previous= ''):
self.index = index
self.timestamp = timestamp
self.data= data
self.previous = previous
self.hash = ''
def calculateHash(self):
return hashlib.sha256(self.index + self.previous + self.timestamp+ (self.data).__str__()
class BlockChain:
def __init__(self):
self.chain= [self.createGenesisBlock()]
def createGenesisBlock(self):
return block(0, "01/01/2017", "Genesis Block", "0")
def getLatestBlock(self):
return self.chain[len(self.chain)-1]
def addBlock(self, newBlock):
newBlock.previous = self.getLatestBlock().hash
newBlock.hash= newBlock.calculateHash()
self.chain.push(newBlock)
korCoin = BlockChain()
korCoin.addBlock(block(1, "10/07/2017", 4))
korCoin.addBlock(block(2, "12/07/2017", 40))
if __name__ == "__main__":
print(korCoin)
遵循python的缩进here
您缺少右括号:
hashlib.sha256(self.index + self.previous + self.timestamp+ (self.data).__str__()
hashlib.sha256(self.index + self.previous + self.timestamp+ (self.data).__str__())
我假设您已经正确缩进了您的实际代码,但在 OP 中粘贴了错误的标识。这就是第一个答案要求您修复缩进的原因。
代码还有其他问题
self.index
是一个整数,你必须在与其他人连接之前将它转换为字符串。您不能连接字符串和整数self.previous
是一个空字符串('') in the first
korCoin.addBlock` 调用但在第二次调用中是一个 Hash 对象。您必须将其转换为字符串或在与其他人连接之前检索其字符串表示self.chain
是一个列表,列表没有push
方法。我想你的意思是append