In this challenge, you are tasked with helping a small, rural town modernize its vote-counting process. (Up until now, Uncle Cleetus had been trustfully tallying them one-by-one, but unfortunately, his concentration isn\'t what it used to be.)
You will be give a set of poll data called election_data.csv. The dataset is composed of three columns: Voter ID, County, and Candidate. Your task is to create a Python script that analyzes the votes and calculates each of the following
The total number of votes cast
A complete list of candidates who received votes
The percentage of votes each candidate won
The total number of votes each candidate won
The winner of the election based on popular vote.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import csv

# dictionary with candidates as keys and votes as values
candidates = dict()

# read the data file
with open("election_data.csv", mode="r") as csv_file:
    elections_data = csv.reader(csv_file, delimiter=',')
    next(elections_data) # skip the header
    for row in elections_data: # iterate over all rows
       candidate = row[2] # candidate name
       # if candidate name is not in the dictionary, update the dictionary
       if candidate not in candidates.keys():
       # increment the number of votes by one
       candidates[candidate] += 1

total_votes = sum(candidates.values())
winner = max(candidates, key=lambda k: candidates[k])

# write results
file_to_output = "resultsPoll.txt"

with open(file_to_output, "w") as txt_file:
    txt_file.write("Election Results...

By purchasing this solution you'll be able to access the following files: and Solution.csv.

for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Python Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats