File
File
class VotingMachine:
Defines the VotingMachine class, which encapsulates the voting logic and data.
Constructor Method
Defines a method to read candidates from a specified file and return them as a
dictionary.
candidates = {}
Splits non-empty lines into candidate number and name, adding them to the
candidates dictionary.
return candidates
Catches and raises specific exceptions related to file handling or other errors.
Vote Method
Checks if the voter has already voted, raising an exception if they have.
Adds the voter's ID to the voters set and increments the count for the selected
candidate.
Iterates through the candidates dictionary and prints each candidate's number and
name.
Main Function
while True:
Prompts the user to enter their voter ID, with options to exit or end voting.
if voter_id.lower() == 'exit': # Exit the loop if the user types 'exit' break elif
voter_id.lower() == 'end': # Finalize voting if the user types 'end' break
Checks for user input to either exit the program or end voting, breaking the loop
if either is typed.
candidate_number = input("Enter the number of the candidate you want to vote for:
")
Prompts the user to enter the candidate number they wish to vote for.
After exiting the loop, calls the method to display the final vote counts.
Entry Point
Checks if the script is run directly (not imported) and calls the main function to
start the program.