Repository navigation
Expand file tree
/
Copy pathPyPoll_Challenge_starter_code.py
More file actions
167 lines (119 loc) · 5.12 KB
/
Copy pathPyPoll_Challenge_starter_code.py
File metadata and controls
167 lines (119 loc) · 5.12 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# -*- coding: UTF-8 -*-
"""PyPoll Homework Challenge Solution."""
# Add our dependencies.
import csv
import os
# Assign a variable for the file to load and path
file_to_load = os.path.join("Resources", "election_results.csv")
# Assign a variable to save file to a path
saved_file = os.path.join("analysis", "election_analysis.txt")
# Initialize total vote counter
total_votes = 0
# Candidate options and votes list
candidate_options = []
candidate_votes = {}
# Listed Counties and votes
counties_list = []
counties_votes = {}
# tracking winning candidate, vote count and percentage.
winnig_candidate = " "
winning_count = 0
winning_percentage = 0
# tracking largest county for challenge
largest_turnout = " "
largest_county_votes = 0
# Open the elecion results to let the file to be read in order to find results
with open(file_to_load) as clrd_election_data:
# read the file object with csv.reader functions
file_reader = csv.reader(clrd_election_data)
print(file_reader)
# read the header row and skip it
headers = next(file_reader)
# using loops to iterate each row to count total votes, unique names, and unique count in the csv file
for row in file_reader:
# adds the total vote count
total_votes += 1
# evaluates candidates from correct coloumn
candidate_name = row[2]
# if candidate does not match existing candidate
if candidate_name not in candidate_options:
# adds the name to the list of candidates
candidate_options.append(candidate_name)
# setting each unique vote count to zero for each candidate
candidate_votes[candidate_name] = 0
# adding vote to a unique candidate by increments
candidate_votes[candidate_name] += 1
#evalutaes county name from correct coloumn
county_name = row[1]
#if county does not match existing county
if county_name not in counties_list:
# adds county name to the list
counties_list.append(county_name)
# setting each county count to zero
counties_votes[county_name] = 0
# adding vote to a unique county by increments
counties_votes[county_name] += 1
# use open function to write data with 'w' on file and save results
with open(saved_file, "w") as textcomp_file:
#print the final vote count to the terminal.
election_results = (
f"\nElection Results\n"
f"{'-'*40}\n"
f"Total Votes: {total_votes:,}\n"
f"{'-'*40}\n"
f"\nCounty Votes: \n")
print(election_results)
# saving the printed results in the text file.
textcomp_file.write(election_results)
#iterating through county list to count votes
for counties in counties_votes:
# retrieve vote count of county
cvotes = counties_votes[counties]
# calculating percentage for each county
county_percentage = float(cvotes)/ float(total_votes) * 100
#Print the county name, vote count and percentage of votes in terminal.
county_results = (
f"{counties}: {county_percentage:.1f}% ({cvotes:,})\n")
print(county_results)
textcomp_file.write(county_results)
# finding the largest county turnout and print the result
if (cvotes > largest_county_votes):
largest_county_votes = cvotes
largest_turnout = counties
#printing and writing the results.
largest_county_votes =(
f"\n{'-'*40}\n"
f"Largest County Turnout: {largest_turnout}"
f"\n{'-'*40}\n")
print(largest_county_votes)
textcomp_file.write(largest_county_votes)
#adding a little spacing and candidate title before tabulating candidate data
spacing =(
f"\nCandidate Votes: \n")
print(spacing)
textcomp_file.write(spacing)
# Iterate through the candidate list.
for candidate in candidate_votes:
# Retrieve vote count of a candidate.
votes = candidate_votes[candidate]
# Calculate the percentage of votes.
vote_percentage = float(votes) / float(total_votes) * 100
# Print the candidate name, vote count and percentage of votes in terminal.
candidate_results = (
f"{candidate}: {vote_percentage:.1f}% ({votes:,})\n")
print(candidate_results)
textcomp_file.write(candidate_results)
# Determine winning vote count, percentage, and candidate by checking if the votes is greater than the winning count
if (votes > winning_count) and (vote_percentage > winning_percentage):
# if true, then will set them equal to each other
winning_count = votes
winning_candidate = candidate
winning_percentage = vote_percentage
winning_candidate_summary = (
f"\n{'-'*40}\n"
f"Winner: {winning_candidate}\n"
f"Winning Vote Count: {winning_count:,}\n"
f"Winning Percentage: {winning_percentage:.1f}%\n"
f"{'-'*40}\n")
print(winning_candidate_summary)
textcomp_file.write(winning_candidate_summary)