-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyPoll.py
109 lines (80 loc) · 3.49 KB
/
PyPoll.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
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
import csv
import os
#Instantiate variable to hold 'election_results.csv' data
csvFile = os.path.join('Resources','election_results.csv')
#Instantiate variable to hold output file
saveFile = os.path.join('Analysis', 'election_analysis.txt')
#Instantiating varable to hold total vote count
totalVotes = 0
#Instantiating list to hold candidate optoins
candidateOptions = []
#Instantiating dict to hold the votes for each candidate
candidateVotes = {}
#Instantiating variables to hold election winner information
winningCandidate = ''
winningCount = 0
winningPercentage = 0
#Open the election results and read the file.
with open(csvFile) as election_data:
#Reading 'election_results.csv' data, skipping the header row
fileReader = csv.reader(election_data)
headers = next(fileReader)
#looping through rows
for row in fileReader:
#Increases 'totalVotes' count by 1
totalVotes += 1
#Assigning candidate name to variable
candidateName = row[2]
#Adds candidate name if not already in the 'candidateOptions' list
if candidateName not in candidateOptions:
candidateOptions.append(candidateName)
#Instantiating 'candidateOptions' list variables to 0
candidateVotes[candidateName] = 0
#Increases vote count for specific candidate by 1
candidateVotes[candidateName] +=1
#Opens 'election_analysis.txt' file output the results to
with open(saveFile, 'w') as txt_file:
#Instantiating 'electionResults' string to hold header and total votes
electionResults = (
f'\nElection Results\n'
f'------------------------------\n'
f'Total Votes: {totalVotes:,}\n'
f'------------------------------\n'
)
#Print 'electionResults' string
print(electionResults, end='')
#Writes 'electionResults' to the 'election_analysis.txt' file
txt_file.write(electionResults)
#Loop to calculate percentages
for candidateName in candidateVotes:
#Instantiating 'votes' variable to hold vote count from specified 'candidateName'
votes = candidateVotes[candidateName]
#Calculates the percentage of votes for each candidate
votePercentage = float(votes) / float(totalVotes) * 100
#Instantiating string to hold 'candidateName','votePercentage','votes'
candidateResults = (
f'{candidateName}: {votePercentage:.1f}% ({votes:,})\n'
)
#Prints 'candidateResults'
print(candidateResults)
#Appends 'candidateResults' to 'election_analysis.txt'
txt_file.write(candidateResults)
#Finds the winner by looping through the candidate results, comparing to see who
#has the highest vote count and percentage.
if(votes > winningCount) and (votePercentage > winningPercentage):
winningCount = votes
winningPercentage = votePercentage
winningCandidate = candidateName
#Instantiating 'winningCandidateSummary' string to hold
#the winners name, vote count and percentage
winningCandidateSummary = (
f'------------------------------\n'
f'Winner: {winningCandidate}\n'
f'Winning Vote Count: {winningCount:,}\n'
f'Winning Percentage: {winningPercentage:.1f}%\n'
f'------------------------------\n'
)
#Prints 'winningCandidateSummary' string
print(winningCandidateSummary)
#Appends 'winningCandidateSummary' to 'election_analysis.txt' file
txt_file.write(winningCandidateSummary)