Syntax Error running on Python

0 votes

I'm trying to follow the blockchain tutorial which is in JS but I'm trying it on Python.

I've got this far and when I'm trying to test run it, I get this syntax error which is confusing me since it seems legit.

Any thoughts?

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)
Sep 21, 2018 in Blockchain by slayer
• 29,350 points
360 views

1 answer to this question.

0 votes

You are missing the closing parenthesis:

hashlib.sha256(self.index + self.previous + self.timestamp+ (self.data).__str__() hashlib.sha256(self.index + self.previous + self.timestamp+ (self.data).__str__())

I am assuming you have properly indented your actual code but pasted it with wrong identation in the OP. That's why the first answer asks you to fix indentation.

There are other issues with the code

  • self.index is an integer, you have to convert it to string before concatenating with others. You can not concatenate a string and integer

  • self.previous is an empty string('') in the firstkorCoin.addBlock` call but is a Hash object in the second call. You have to convert it to string or retrieve its string representation before concatenating with others

  • self.chain is a list and list doesn't have push method. I suppose you meant append
answered Sep 21, 2018 by digger
• 26,740 points

Related Questions In Blockchain

0 votes
1 answer

Blockchain Python Syntax Error

Look here:  print('Block #{} has been added to ...READ MORE

answered Jul 13, 2018 in Blockchain by Christine
• 15,790 points
704 views
0 votes
2 answers

Python syntax error because of version conflicts.

As mentioned in the reply to my ...READ MORE

answered Aug 13, 2018 in Blockchain by Omkar
• 69,210 points
936 views
0 votes
1 answer

Error while running make command using Fabric 1.0.6

It's a known issue in go 1.9.4: https://github.com/golang/go/issues/23739 While ...READ MORE

answered Jun 27, 2018 in Blockchain by Perry
• 17,100 points
781 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,663 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,215 views
0 votes
2 answers
0 votes
1 answer

coin.mx : 403 forbidden error on my web request

I was having the same issue and ...READ MORE

answered Sep 11, 2018 in Blockchain by digger
• 26,740 points
852 views
0 votes
2 answers

Trying to issue tokens on testnet with python API

By looking at - documentation here I think you ...READ MORE

answered Sep 17, 2018 in Blockchain by Khush
514 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP