Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I've recently been building a blockchain in Python. In my
Block
class, I initialise a
nonce
variable, which is then used later on in the class. However, when I try to use the variable elsewhere in the class, I get
AttributeError: 'Block' object has no attribute 'nonce'
. Here is my code:
from hashlib import sha256
class Block():
def __init__(self, data=None):
self.timestamp = str(time.time())
self.data = data
self.previousHash = '0'*64
self.hash = self.calculateHash()
self.nonce = 0
self.dict = {
"Previous hash": self.previousHash,
"Hash": self.hash,
"Timestamp": self.timestamp,
"Data": self.data
def mineBlock(self, difficulty):
while self.hash[:difficulty] != '0'*difficulty:
self.nonce += 1
self.calculateHash()
print('Block mined: ' + self.hash)
def calculateHash(self):
hashingText = f'{self.timestamp}{self.data}{self.previousHash}{self.nonce}'.encode(
'utf-8')
return sha256(hashingText).hexdigest()
Here is the last part of the error, in case it helps:
File "/Users/[MYNAME]/Developer/DigiGov/src/BlockClass.py", line 28, in calculateHash
hashingText = f'{self.timestamp}{self.data}{self.previousHash}{self.nonce}'.encode(
AttributeError: 'Block' object has no attribute 'nonce'
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.