The goal of this project is to summarize a set of voting data across three counties and across three potential candidates. The necessary outcomes are to find the winner of the election across all counties, the county with the highest turnout, and summaries of candidate and voter turnout records by county. The summary output needs to be printed to both the terminal and to a text output file.
The summary output can be seen in the following image, capture from the terminal output. A breakdown of how each piece was derived is below the image.
-
-
Result: 369,711
-
Process: Define a variable (total_votes), set it to 0 initially, and then include a counter that increments by 1 for each row of vote count data
for row in reader: # Add to the total vote count total_votes = total_votes + 1
-
-
- Result:
- Jefferson: 10.5% (38,855)
- Denver: 82.8% (306,055)
- Arapahoe: 6.7% (24,801)
- Process: To summarize data by county, there are multiple steps involved
-
Create a list (county_list) to store the different county names
county_list = []
-
Define where in header row to extract county name data (using index)
county_name = row[1]
-
As new county names are encountered by row, add them (append) to the county name list
if county_name not in county_list: county_list.append(county_name)
-
As new county names are added, beging tracking their votes within each county, incrementing by 1 for each found row
county_votes[county_name] = 0 county_votes[county_name] += 1
-
- Result:
-
-
Results: Denver
-
Process:
-
Write a for loop to get the county from the county dictionary and retrieve the vote count.
for county_name in county_votes: votes = county_votes.get(county_name)
-
Calculate the percentage of votes for the county.
vote_percentage = float(votes) / float(total_votes) * 100 county_results = ( f"{county_name}: {vote_percentage:.1f}% ({votes:,})\n")
-
Write an if statement to determine the winning county and get its vote count.
if (votes > largest_county) and (vote_percentage > turnout_percentage): largest_county = votes winning_county = county_name turnout_percentage = vote_percentage
-
-
-
- Results:
- Chales Casper Stockham: 23.0% (85,213)
- Diana DeGette: 73.8% (272,892)
- Raymon Anthony Doane: 3.1% (11,606)
- Process: The steps were similar to the above process in determing county totals and percentages, but with a focus on the Candidate column of data instead of County.
- Results:
Which candidate won the election, what was their vote count, and what was their percentage of the total votes?
-
Results: Diana DeGette won the election, receiving 73.8% (272,892) of total votes
-
Process: Again, this process was similar with how the highest county voter turnout was arrived at. However, the code was slightly different since it didn't focus on a county level breakout for the overall winner of the election.
for candidate_name in candidate_votes: votes = candidate_votes.get(candidate_name) vote_percentage = float(votes) / float(total_votes) * 100 candidate_results = ( f"{candidate_name}: {vote_percentage:.1f}% ({votes:,})\n") if (votes > winning_count) and (vote_percentage > winning_percentage): winning_count = votes winning_candidate = candidate_name winning_percentage = vote_percentage
The steps followed in this code could be used to derive the outcome of any election, since the basice pseudocode would be the same:
- Count up the total votes by candidate to find the winner (can also summarize the percentage of votes as well as absolute total vote count)
- Tally up the votes by county to find where the highest turnout occurred (similarly, this can be expressed as a percentage or total vote count)