-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyPoll.py
More file actions
89 lines (71 loc) · 3.03 KB
/
Copy pathPyPoll.py
File metadata and controls
89 lines (71 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Open the data file.
# Write down the names of all the candidates.
# Add a vote count for each candidate.
# Get the total votes for each candidate.
# Get the total votes cast for the election.
# 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")
# 1. Initialize a total vote counter.
total_votes = 0
# Candidate Options
candidate_options = []
#Create a dictionary to store candidate name and its vote count
candidate_votes = {}
# Winning Candidate and Winning Count Tracker
winning_candidate = ""
winning_count = 0
winning_percentage = 0
# Open the election results and read the file.
with open(file_to_load) as election_data:
# To do: read and analyze the data here.
# Read the file object with the reader function.
file_reader = csv.reader(election_data)
# Read and print the header row.
headers = next(file_reader)
print(headers)
# # Print each row in the CSV file.
for row in file_reader:
# 2. Add to the total vote count.
total_votes = total_votes + 1
# Print the candidate name from each row.
candidate_name = row[2]
# If the candidate does not match any existing candidate...
if candidate_name not in candidate_options:
# Add it to the list of candidates.
candidate_options.append(candidate_name)
# 2. Begin tracking that candidate's vote count.
candidate_votes[candidate_name] = 0
# Add a vote to that candidate's count.
candidate_votes[candidate_name] = candidate_votes[candidate_name] + 1
#Print total votes
print(f"The total votes were {total_votes}")
# Determine the percentage of votes for each candidate by looping through the counts.
# 1. Iterate through the candidate list.
for candidate_name in candidate_votes:
# 2. Retrieve vote count of a candidate.
votes = candidate_votes[candidate_name]
# 3. Calculate the percentage of votes.
vote_percentage = float(votes) / float(total_votes) * 100
# 4. Print the candidate name and percentage of votes.
print(f"{candidate_name}: received {vote_percentage:.1f}% of the vote.")
# Determine winning vote count and candidate
# Determine if the votes is greater than the winning count.
if (votes > winning_count) and (vote_percentage > winning_percentage):
# If true then set winning_count = votes and winning_percent =
# vote_percentage.
winning_count = votes
winning_percentage = vote_percentage
# And, set the winning_candidate equal to the candidate's name.
winning_candidate = candidate_name
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)