0% found this document useful (0 votes)
11 views2 pages

Man Than

The document outlines a simple blockchain voting system implemented in Python. It defines a Block class to create individual blocks containing voter information and a Blockchain class to manage the chain of blocks. The system allows users to input their name and select a candidate to record their vote, which is then stored in the blockchain.

Uploaded by

anshbatham75
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Man Than

The document outlines a simple blockchain voting system implemented in Python. It defines a Block class to create individual blocks containing voter information and a Blockchain class to manage the chain of blocks. The system allows users to input their name and select a candidate to record their vote, which is then stored in the blockchain.

Uploaded by

anshbatham75
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import hashlib

import datetime as dt

class Block:
def __init__(self, index, timestamp, voter, candidate, previous_hash):
self.index = index
self.timestamp = timestamp
self.voter = voter
self.candidate = candidate
self.previous_hash = previous_hash
self.hash = self.calculate_hash()

def calculate_hash(self):
sha = hashlib.sha256()
sha.update(str(self.index).encode('utf-8') +
str(self.timestamp).encode('utf-8') +
self.voter.encode('utf-8') +
self.candidate.encode('utf-8') +
self.previous_hash.encode('utf-8'))
return sha.hexdigest()

class Blockchain:
def __init__(self):
self.chain = [self.create_genesis_block()]

def create_genesis_block(self):
return Block(0, dt.datetime.now(), "Genesis Voter", "Genesis Candidate",
"0")

def add_block(self, voter, candidate):


previous_block = self.chain[-1]
new_block = Block(len(self.chain), dt.datetime.now(), voter, candidate,
previous_block.hash)
self.chain.append(new_block)

def print_chain(self):
for block in self.chain:
print("Index:", block.index)
print("Timestamp:", block.timestamp)
print("Voter:", block.voter)
print("Candidate:", block.candidate)
print("Previous Hash:", block.previous_hash)
print("Hash:", block.hash)
print()

if __name__ == "__main__":
blockchain = Blockchain()

print("Welcome to the Blockchain Voting System!")


print("Candidates: BJP, Congress, Manthan, Ram, Aam Aadmi Party")

while True:
voter_name = input("Enter voter name (or 'exit' to finish): ")
if voter_name.lower() == 'exit':
break

print("Choose a candidate:")
print("1. BJP")
print("2. Congress")
print("3. Manthan")
print("4. Ram")
print("5. Aam Aadmi Party")
candidate_choice = input("Enter your choice (1-5): ")

candidates = ["BJP", "Congress", "Manthan", "Ram", "Aam Aadmi Party"]


if candidate_choice.isdigit() and 1 <= int(candidate_choice) <= 5:
candidate_name = candidates[int(candidate_choice) - 1]
blockchain.add_block(voter_name, candidate_name)
print("Vote recorded!\n")
else:
print("Invalid choice! Please choose again.\n")

print("\nVoting has concluded. Here is the blockchain:")


blockchain.print_chain()

You might also like