Code Bug Fix: Python blockchain. NameError: name ‘Chain’ is not defined

Original Source Link

Today I was building a blockchain in python off of this tutorial, since I’m interested in cryptocurrency. I was ready to launch it, when I got this error.

Traceback (most recent call last):
File "blockchain.py", line 9, in <module>
class Chain(object):
File "blockchain.py", line 17, in Chain
blockchain = Chain()
NameError: name 'Chain' is not defined

I don’t really know why this is happening, to me there doesn’t really seem to be a problem. Here is part of the code where it is having the error:

import hashlib
import json
from textwrap import dedent
from time import time
from hashlib import sha256
from uuid import uuid4
from flask import Flask, jsonify, request

class Chain(object):
  def __init__(self):
      self.chain = []
      self.current_transactions = []
      self.new_block(previous_hash=1, proof=100)

  app = Flask(__name__)
  node_indentifier = str(uuid4()).replace('-', '')
  blockchain = Chain()

If you need more of the code then I don’t mind giving more.
Thanks in advance!

IIUC you are trying to call the blockchain = Chail() outside the class Chain. Your indentation is wrong. This should work

class Chain(object):
  def __init__(self):
      self.chain = []
      self.current_transactions = []
      self.new_block(previous_hash=1, proof=100)

app = Flask(__name__)
node_indentifier = str(uuid4()).replace('-', '')
blockchain = Chain()

Tagged :

Leave a Reply

Your email address will not be published. Required fields are marked *