-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainPP.py
More file actions
74 lines (58 loc) · 2.37 KB
/
mainPP.py
File metadata and controls
74 lines (58 loc) · 2.37 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
#PyPoll
import os
import csv
# set a csv file path for the data
poll_csv = os.path.join('election_data.csv')
# define function
def get_results(data):
# define variables
totalVotesCount = 0
votes = []
candidateCount = []
uniqueCandidates = []
percent = []
# start looping through rows
for row in data:
# count the total number of votes
totalVotesCount += 1
# append unique names to the candidates list
if row[2] not in uniqueCandidates:
uniqueCandidates.append(row[2])
# make a list of all the votes
votes.append(row[2])
# start a second loop that will populate the candidateCount with each vote
for candidate in uniqueCandidates:
candidateCount.append(votes.count(candidate))
percent.append(round(votes.count(candidate)/totalVotesCount*100,3))
# find the winner using index position of the max count in candidateCount
winner = uniqueCandidates[candidateCount.index(max(candidateCount))]
# print results, use a loop for the number of uniqueCandidates
print('Election Results')
print('--------------------------------')
print(f'Total Votes: {totalVotesCount}')
print('--------------------------------')
for i in range(len(uniqueCandidates)):
print(f'{uniqueCandidates[i]}: {percent[i]}% {candidateCount[i]}')
print('--------------------------------')
print(f'Winner: {winner}')
print('--------------------------------')
# set exit path
poll_output = os.path.join("PyPollResults.txt")
# write out results to text file
with open(poll_output, "w") as txtfile:
txtfile.write('Election Results')
txtfile.write('\n------------------------------------')
txtfile.write(f'\nTotal Votes: {totalVotesCount}')
txtfile.write('\n------------------------------------')
for i in range (len(uniqueCandidates)):
txtfile.write(f'\n{uniqueCandidates[i]}: {percent[i]}% {candidateCount[i]}')
txtfile.write('\n------------------------------------')
txtfile.write(f'\nWinner: {winner}')
txtfile.write('\n------------------------------------')
# read in the CSV file
with open(poll_csv, newline='') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',')
# adjust for header
csv_header = next(csvfile)
# use function
get_results(csvreader)