Conducting Central Elections Using Python

Aviral Srivastava Avatar

Introduction: In today’s technologically driven world, automating processes has become a norm. When it comes to democratic processes like elections, leveraging programming languages like Python can streamline the process, making it more efficient and transparent. In this blog post, we’ll explore how to conduct a central election using Python, from setting up the code to understanding its working.

Setting up the Code: We have two main Python scripts for this election process:

  1. Voting System:
# Importing the voterlist module
from voterlist import voterlist
import os

# Infinite loop for continuous voting until all voters have cast their votes
while True:
    if voterlist != []:
        # Displaying welcome message for the Central Elections 2024
        print("Welcome to Central Elections 2024")
        
        # Taking input from the voter
        voter = input("Input your name to VOTE: ")
        
        # Verifying if the voter is in the voterlist
        if voter in voterlist:
            # Displaying welcome message to the voter
            print("Welcome,", voter, ",Use your vote wisely")
            
            # Displaying voting options
            print("Enter 1 for BJP \nEnter 2 for INDIA \nEnter 3 for Rustic Spiritual \nFor NOTA press 0")
            
            # Taking vote input from the voter
            vote = int(input("Please Enter any ONE option from the above according to your choice: "))
            
            # Processing the vote based on the input
            if vote == 1:
                # Opening and reading the BJP vote count file
                file = open("bjp.txt", "r") 
                current_vote = int(file.read())
                file.close()
                
                # Updating the BJP vote count
                file = open("bjp.txt", "w")
                new_vote = str(current_vote + 1)
                file.write(new_vote)
                file.close()
                
                # Informing the voter about successful vote submission
                print("Your vote is successfully submitted to BJP")
                
                # Removing the voter from the voterlist after casting the vote
                voterlist.remove(voter)
            
            # Processing vote for INDIA party
            elif vote == 2:
                # Similar process as above for other parties
                ...
            # Processing vote for Rustic Spiritual party
            elif vote == 3:
                ...
            # Processing vote for NOTA
            elif vote == 0:
                ...
        else:
            ...
    if voterlist == []:
        break

# Displaying completion message once voting is completed
print("Voting Is Completed!!")

2. Election Result Checker:

# Opening and reading the vote count files for each party
f = open("BJP.txt", "r")
bjp = int(f.readline())
f.close()

f = open("INDIA.txt", "r")
india = int(f.readline())
f.close()

f = open("Rustic Spiritual.txt", "r")
rs = int(f.readline())
f.close()

# Comparing the vote counts to determine the winner or if there's a tie
if bjp > india and bjp > rs:
    # If BJP has the highest votes
    print("BJP Won 2024 Central Elections by", bjp, 'votes')
    print("INDIA=", india)
    print("Rustic Spiritual=", rs)
elif bjp < india and india > rs:
    # If INDIA has the highest votes
    print("INDIA Won 2024 Central Elections by", india, 'votes')
    print("BJP=", bjp)
    print("Rustic Spiritual=", rs)
elif bjp < rs and india < rs:
    # If Rustic Spiritual has the highest votes
    print("Rustic Spiritual Won 2024 Central Elections by", rs, 'votes')
    print("BJP=", bjp)
    print("INDIA=", india)
else:
    # Handling tie scenarios
    if bjp == rs and rs == india:
        print("All Parties have the same number of votes:", rs)
        print("Do re-election")
    elif bjp == rs:
        print("BJP and Rustic Spiritual have the same number of votes:", rs)
        print("Do re-election between BJP and Rustic Spiritual only")
    elif bjp == india:
        print("BJP and INDIA have the same number of votes:", bjp)
        print("Do re-election between BJP and INDIA only")
    elif india == rs:
        print("INDIA and Rustic Spiritual have the same number of votes:", rs)
        print("Do re-election between INDIA and Rustic Spiritual only")

File Setup: Before running the scripts, we need to set up the following files:

  1. voterlist.py: This file contains the list of eligible voters. It should be a Python module named voterlist.py, with a list named voterlist containing the names of eligible voters.
# voterlist.py

# List of eligible voters
voterlist = ["Aviral", "Sam", "Shreyaaawww", "Chota Chuha", "Silli Billi"]




  1. BJP.txt, INDIA.txt, Rustic Spiritual.txt: These files will store the vote count for each party. Initially, they should contain “0” (zero) as the starting vote count.

Conclusion: By following these steps, we can set up a central election system using Python. The Voting System script manages the voting process, while the Election Result Checker script determines the winner based on the recorded votes. With the power of Python, we can conduct transparent, efficient, and fair elections, contributing to the democratic process in a technologically advanced manner. Happy coding and happy voting!


Leave a comment

Design a site like this with WordPress.com
Get started