QuestionQuestion

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

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse 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():
            candidates.update({candidate:0})
       # 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...
$15.00 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.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
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