-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyPoll.py
More file actions
206 lines (148 loc) · 5.87 KB
/
PyPoll.py
File metadata and controls
206 lines (148 loc) · 5.87 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#The data we need to retrieve.
from email import header
import os
import csv
# Set path for file
file_to_load = 'Resources/election_results.csv'
election_data = open(file_to_load, 'r')
print(election_data)
election_data.close()
with open(file_to_load) as election_data:
print(election_data)
file_to_load = os.path.join("Resources", "election_results.csv")
with open(file_to_load) as election_data:
print(election_data)
# Create a filename variable to a direct or indirect path to the file.
file_to_save = os.path.join("analysis", "election_analysis.txt")
outfile =open(file_to_save, "w")
outfile.write("Hello World")
outfile.close()
file_to_save = os.path.join("analysis", "election_analysis.txt")
with open(file_to_save, "w") as txt_file:
#txt_file.write("Hello World")
#txt_file.write("Aparahoe, ")
#txt_file.write("Denver, ")
#txt_file.write("Jefferson, ")
#txt_file.write("El Paso")
#Write three counties to the file
#txt_file.write("Aparahoe, Denver, Jefferson")
txt_file.write("Counties in the Election\n------------------------\nAparahoe\nDenver\nJefferson")
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 = []
# Declare the empty dictionary
candidate_votes ={}
#open the election results and read the file.
with open(file_to_load) as election_data:
#read the file object with the reader function
file_reader = csv.reader(election_data)
#Read the header now
headers =next(file_reader)
#print(headers)
#print each row in the CSV file.
for row in file_reader:
#print(row)
#Add to the total vote count.
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 the cadidate name to the candidate list.
candidate_options.append(candidate_name)
#begin tracking that candidates's vote count.
candidate_votes[candidate_name]= 0
#Add a vote to that candidate's count.
candidate_votes[candidate_name] += 1
print(candidate_votes)
#1 Iterate through the candidate list:
for candidate_name in candidate_votes:
votes = candidate_votes[candidate_name]
vote_percentage = float(votes) / float(total_votes) *100
print(f"{candidate_name}: received {round(vote_percentage, 2)}% of the vote.")
#3. Print the total votes.
#print(total_votes)
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 = []
# Declare the empty dictionary
candidate_votes ={}
winning_candidate = ""
winning_count = 0
winning_percentage = 0
#open the election results and read the file.
with open(file_to_load) as election_data:
#read the file object with the reader function
file_reader = csv.reader(election_data)
#Read the header now
headers =next(file_reader)
#print(headers)
#print each row in the CSV file.
for row in file_reader:
#print(row)
#Add to the total vote count.
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 the cadidate name to the candidate list.
candidate_options.append(candidate_name)
#begin tracking that candidates's vote count.
candidate_votes[candidate_name]= 0
#Add a vote to that candidate's count.
candidate_votes[candidate_name] += 1
#print(candidate_votes)
with open(file_to_save, "w") as txt_file:
election_results = (
f"\nElection Results\n"
f"------------------------\n"
f"Total Votes: {total_votes:,}\n"
f"------------------------\n")
print(election_results, end="")
txt_file.write(election_results)
#txt_file.write(candidate_results)
#1 Iterate through the candidate list:
for candidate_name in candidate_votes:
votes = candidate_votes[candidate_name]
vote_percentage = float(votes) / float(total_votes) *100
#print(f"{candidate_name}: received {round(vote_percentage, 2)}% of the vote.")
# determine winning vote count and candidate
# determine if the votes are greater than the winning count
if (votes > winning_count) and (vote_percentage > winning_percentage):
#if true then set winning_count = votes and winning_percentage =
# vote_percentage
winning_count = votes
winning_percentage =vote_percentage
# set the winning_candidate equal to the candidate's name.
winning_candidate = candidate_name
candidate_results = (f"{candidate_name}: {vote_percentage:.1f}% ({votes:,})\n")
txt_file.write(candidate_results)
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)
txt_file.write(winning_candidate_summary)
#csvpath = os.path.join("..", "Resources", "election_results.csv")
# 1. The total number of votes cast
# 2. Complete list of candidates who received votes
# 3. The percentage of votes each candidate won
# 4. The total number of votes each candidate won
# 5. The winner of the election based on popular vote