缺少一个必需的位置参数

问题描述

我在做一个区块链项目。在我添加了挖掘功能之前,它似乎没有任何问题。然后它吐出两个错误,它缺少位置参数。第一个是第 45 行的 main 函数,第二个是 if name 是 main 函数。这使得自。它与挖掘功能有关。代码有点冗长且难以理解,所以我保留了必要的部分。

from hashlib import sha3_512

##some hashing stuff
class Block():
## the variables thrown into the function
    def __init__(self,txdata,number):
         #initialize function.
    # More hashing stuff
class blockchain:
    difficulty = 4
    def __init__(self,chain=[]):
        #Initialize function. Sets up the blockchain
    def add(self,Block):
        #lables adding stuff to the chain
    def mine(self,Block):
        try:
            block.prev = self.chain[-1].get('hash')
        except IndexError:
            pass
        while True:
            if Block.hash()[:blockchain.difficulty] == "0"*blockchain.difficulty:
                self.add(Block)
                break
            else:
                Block.nonce +=1
def main():
    blockchain()
    bchain = ["Nice day stack overflow","Once again I need help with my bad code"]
    Num = 0
    for txdata in bchain:
        Num += 1
        blockchain.mine(Block(Block.txdata,Num)) #the error popped up here. I don't get it. I tried fiddling around with it,nothing.
if __name__ == '__main__':
    main()

你能帮我解决这个错误吗?如果不修复,它可能会破坏整个项目。 错误

Traceback(most recent call last)
 File "blockchain.py",line 47 in <module>
   main()
 File "blockchain.py",line 45,in main()
   blockchain.mine(Block(Block.txdata,Num))
Type error: mine missing one positional argument: Block

解决方法

您需要保持 blockchain() 的实例。

def main():
    b = blockchain()  # keep hold of the instance in b
    bchain = ["Nice day stack overflow","Once again I need help with my bad code"]
    Num = 0
    for txdata in bchain:
        Num += 1
        b.mine(Block(Block.txdata,Num))   # Use the instance here
if __name__ == '__main__':
    main()