Blockchain Python Syntax Error

0 votes

Following is my code:

import hashlib as hasher
import datetime as date

class Block:
  def __init__(self, index, timestamp, data, previous_hash):
    self.index = index
    self.timestamp = timestamp
    self.data = data
    self.previous_hash = previous_hash
    self.hash = self.hash_block()

  def hash_block(self):
    sha = hasher.sha256()
    sha.update(str(self.index) + 
               str(self.timestamp) + 
               str(self.data) + 
               str(self.previous_hash))
    return sha.hexdigest()

def create_genesis_block():
  return Block(0, date.datetime.now(), "Genesis Block", "0")

def next_block(last_block):
  this_index = last_block.index + 1
  this_timestamp = date.datetime.now()
  this_data = "Hey! I'm block " + str(this_index)
  this_hash = last_block.hash
  return Block(this_index, this_timestamp, this_data, this_hash)

blockchain = [create_genesis_block()]
previous_block = blockchain[0]

num_of_blocks_to_add = 20

for i in range(0, num_of_blocks_to_add):
  block_to_add = next_block(previous_block)
  blockchain.append(block_to_add)
  previous_block = block_to_add
  print 'Block #{} has been added to the blockchain!'.format(block_to_add.index)
  print "Hash: {}\n".format(block_to_add.hash)

I am getting an error in this line:

print 'Block #{} has been added to the blockchain!'.format(block_to_add.index)

Please help

Jul 13, 2018 in Blockchain by sabby
• 4,370 points
887 views

1 answer to this question.

0 votes

Look here: 

print('Block #{} has been added to the blockchain!'.format(block_to_add.index))

Use print as a function for Python 2.7 or higher.

Or you can also change that code line to this:

print('Block #%s has been added .... blockchain' %(block_to_add.index))
answered Jul 13, 2018 by Christine
• 15,790 points

Related Questions In Blockchain

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,220 points
1,333 views
0 votes
1 answer

Syntax Error running on Python

You are missing the closing parenthesis: hashlib.sha256(self.index + ...READ MORE

answered Sep 21, 2018 in Blockchain by digger
• 26,740 points
524 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,922 views
+1 vote
1 answer
0 votes
1 answer

Python request module for bitcoin json rpc

This should work: #!/usr/bin/env python import getpass import json import requests ...READ MORE

answered Aug 28, 2018 in Blockchain by digger
• 26,740 points
2,362 views
+1 vote
3 answers

Removing double quotes from a string from JSON response in PHP

Just remove the json_encode call, and it should work: $resp ...READ MORE

answered Sep 12, 2018 in Blockchain by digger
• 26,740 points
45,218 views
+1 vote
1 answer

Error while deploying chaincode in IBM Blockchain. Please help.

Try stripping out the 'tree/master' portion of ...READ MORE

answered Apr 26, 2018 in Blockchain by Christine
• 15,790 points
896 views
0 votes
1 answer

How to resolve Blockchain DNS using Python Requests?

Look at this example: import requests from random import ...READ MORE

answered Aug 22, 2018 in Blockchain by Christine
• 15,790 points
1,430 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