Instructions
Objective
Write a program to create a polling challenge in python language.
Requirements and Specifications
Assignment
Download the PyPoll_Challenge_starter_code.py file and rename it PyPoll_Challenge.py.
Use the step-by-step instructions below to add code where indicated by the numbered comments in the starter code file.
Deliverable 1:
Step 1:
- Initialize a county list, like the candidate_options list, that will hold the names of the counties.
- Initialize a dictionary, like the candidate_votes dictionary, that will hold the county as the key and the votes cast for each county as the values.
Step 2:
- Initialize an empty string, like winning_candidate, that will hold the county name for the county with the largest turnout.
- Initialize a variable, like the winning_count variable, that will hold the number of votes of the county that had the largest turnout.
Step 3:
- While reading the election results from each row inside the for loop, write a script that gets the county name from each row.
Step 4a:
- Write a decision statement with a logical operator to check if the county name acquired in Step 3 is in the county list you created in Step 1.
Step 4b:
- If the county is not in the list created in Step 1, add it to the list of county names like you did when adding a candidate to the candidate_options list.
Step 4c:
- Write a script that initializes the county vote to zero, like you did when you began to track the vote counts for the candidates.
Step 5:
- Write a script that adds a vote to the county’s vote count as you are looping through all the rows, like you did for the candidate’s vote count.
Step 6a:
- Write a repetition statement to get the county from the county dictionary that was created in Step 1.
Step 6b:
- Initialize a variable to hold the county’s votes as they are retrieved from the county votes dictionary.
Step 6c:
- Write a script that calculates the county’s votes as a percentage of the total votes.
Step 6d:
- Write a print statement that prints the current county, its percentage of the total votes, and its total votes to the command line.
Step 6e:
- This step will be completed in Deliverable 2.
Step 6f:
- Write a decision statement that determines the county with the largest vote count and then adds that county and its vote count to the variables created in Step 2.
Step 7:
- Write a print statement that prints out the county with the largest turnout.
DELIVERABLE 2:
Step 6e:
- Write a script that saves each county, the county’s total votes, and the county’s percentage of total votes to the election_results.txt file.
Step 8:
- Write a script that saves the county with the largest turnout to the election_results.txt file.
- After you complete your python assignment run your solution to Deliverable 2, confirm that your election_results.txt file matches the following image:
Source Code
# The data we need to retrieve
# 1. The total number of votes cast
# 2. A complete list of candidates who recieved votes
# 3. Percentage of votes each candidate won
# 4. Total number of votes each candidate won
# 5. The winner of the election based on popular vote
# Add our dependencies.
import csv
import os
# Assign a variable to load a file from a path.
file_to_load = os.path.join("Resources", "election_results.csv")
# Assign a variable to save the file to a path.
file_to_save = os.path.join("analysis", "election_analysis.txt")
# Initialize a total vote counter.
total_votes = 0
# Candidate options and candidate votes.
candidate_options = []
candidate_votes = {}
# Track the winning candidate, vote count, and percentage.
winning_candidate = ""
winning_count = 0
winning_percentage = 0
# Open the election results and read the file.
with open(file_to_load) as election_data:
file_reader = csv.reader(election_data)
# Read the header row.
headers = next(file_reader)
# Print each row in the CSV file.
for row in file_reader:
# Add to the total vote count.
total_votes += 1
# Get the candidate name from each row.
candidate_name = row[2]
# If the candidate does not match any existing candidate, add the
# the candidate list.
if candidate_name not in candidate_options:
# Add the candidate name to the candidate list.
candidate_options.append(candidate_name)
# And begin tracking that candidate's voter count.
candidate_votes[candidate_name] = 0
# Add a vote to that candidate's count.
candidate_votes[candidate_name] += 1
# Save the results to our text file.
with open(file_to_save, "w") as txt_file:
# After opening the file print the final vote count to the terminal.
election_results = (
f"\nElection Results\n"
f"-------------------------\n"
f"Total Votes: {total_votes:,}\n"
f"-------------------------\n")
print(election_results, end="")
# After printing the final vote count to the terminal save the final vote count to the text file.
txt_file.write(election_results)
for candidate_name in candidate_votes:
# Retrieve vote count and percentage.
votes = candidate_votes[candidate_name]
vote_percentage = float(votes) / float(total_votes) * 100
candidate_results = (
f"{candidate_name}: {vote_percentage:.1f}% ({votes:,})\n")
# Print each candidate's voter count and percentage to the terminal.
print(candidate_results)
# Save the candidate results to our text file.
txt_file.write(candidate_results)
# Determine winning vote count, winning percentage, and winning candidate.
if (votes > winning_count) and (vote_percentage > winning_percentage):
winning_count = votes
winning_candidate = candidate_name
winning_percentage = vote_percentage
# Print the winning candidate's results to the terminal.
winning_candidate_summary = (
f"-------------------------\n"
f"Winner: {winning_candidate}\n"
f"Winning Vote Count: {winning_count:,}\n"
f"Winning Percentage: {winning_percentage:.1f}%\n"
f"-------------------------\n")
print(winning_candidate_summary)
# Save the winning candidate's results to the text file.
txt_file.write(winning_candidate_summary)
Similar Samples
Struggling with programming assignments? Our experts are here to help! We offer personalized assistance tailored to your unique needs, ensuring you understand the concepts and complete your assignments on time. Let us take the stress out of your studies and guide you to success with our professional support.
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python