-
Notifications
You must be signed in to change notification settings - Fork 0
/
election.py
72 lines (63 loc) · 2.39 KB
/
election.py
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
import os
import csv
csvpath = "election_data.csv"
# open csv file
with open(csvpath, encoding="utf8", newline='') as csvfile:
# CSV reader specifies delimiter and variable that holds contents
csvhand = csv.reader(csvfile, delimiter=',')
csv_header = next(csvfile)
# print(f"CSV Header: {csv_header}")
# declarations
voter = [] # list of voter IDs
candidate = [] # list of candidate names
count = int() # total number of votes
count = 0
percents = [] # repository for calculating percentages of total votes
d = {} # dictionary with vote totals per candidate
for row in csvhand:
# Add voter ID
voter.append(row[0])
count = count + 1
# Add Candidate
candidate.append(row[2])
# Get number of votes per candidate ("histogram") and determine the max
for cand in candidate:
d[cand] = d.get(cand,0) + 1
winnum = max(d.values())
for k, v in d.items():
if v == winnum:
winner = k
# if you don't care about the brackets use print statement below instead of 2nd for
# print([k for k, v in d.items() if v == winner])
# Calculate percentages of vote and save to list percents
for x in d.values():
pct = (100*x)/count
pct = round(pct,3)
percents.append(pct)
# Create lists of candidates and tallies that is indexable
cands = list(d.keys())
tallies = list(d.values())
# Print results to terminal
print("Election Results")
print("-------------------------")
print("Total Votes: ", count)
print("-------------------------")
print(cands[0], ":", percents[0], "%", "(", tallies[0], ")")
print(cands[1], ":", percents[1], "%", "(", tallies[1], ")")
print(cands[2], ":", percents[2], "%", "(", tallies[2], ")")
print(cands[3], ":", percents[3], "%", "(", tallies[3], ")")
print("-------------------------")
print("Winner: ", winner)
# print to text file
import sys
sys.stdout = open('output_poll.txt','w')
print("Election Results")
print("-------------------------")
print("Total Votes: ", count)
print("-------------------------")
print(cands[0], ":", percents[0], "%", "(", tallies[0], ")")
print(cands[1], ":", percents[1], "%", "(", tallies[1], ")")
print(cands[2], ":", percents[2], "%", "(", tallies[2], ")")
print(cands[3], ":", percents[3], "%", "(", tallies[3], ")")
print("-------------------------")
print("Winner: ", winner)