Question: I need help number # 5 and # 7. Here is the code to homework. I tried doing a while loop. import hashlib import datetime

I need help number # 5 and # 7. Here is the code to homework. I tried doing a while loop.

import hashlib

import datetime

# 1. change the Block constructor so that rather than supplying a dateTime stamp

# when creating a block object, use the computer's current date & time stamp

# by using the .now() function for the datetime module

def main():

print("Building a blockchain ...")

#2. create a list that will hold a collection of blocks to form a blockchain

blockchainlist=[]

#3. create your first block with the index 1, the current date & time,

# "Initial Block" for the data, and zero for the previous hash (since there isn't one)

bc = Block(1,datetime.datetime.now(),"Initial Block",0)

blockchainlist.append(bc)

#4. display the hash dycreated for the initial block

print(bc.hash_block())

#5. use a loop to create additional blocks for this blockchain

# the loop should ask the user to provide some data

# add the block (with the user's data) to the blockchain

# display the newly created block's hash

# exit the loop when the user enters DONE for data

info_needed =input("Please provide data")

while x =! "DONE" or x=! "done":

#7. after collecting user data, display the details of the blockchain

# before completing this step, create a

# what a Block is

class Block:

#constructor for a Block - need to make 1 change to this (see #1)

def __init__(self, index,timestamp, data, previous_hash):

self.index = index

self.timestamp = datetime.datetime.now()

self.data = data

self.previous_hash = previous_hash

self.hash = self.hash_block()

#a method to create a hash for a block object

#a hash is an encoded - no need to change this method

def hash_block(self):

sha = hashlib.sha256()

sha.update(str(self.index).encode('utf-8') + str(self.timestamp).encode('utf-8') + str(self.data).encode('utf-8') + str(self.previous_hash).encode('utf-8'))

return sha.hexdigest()

#6. add a method to print a hash object where the index, time created, and data are displayed

def info(self):

print(self.index, self.timestamp,self.data,self.previous_hash, self.hash, sha.hexdigest())

main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!