-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_pypoll-Final.DMJ.py
More file actions
112 lines (90 loc) · 4.26 KB
/
Copy pathmain_pypoll-Final.DMJ.py
File metadata and controls
112 lines (90 loc) · 4.26 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
# Import Modules
import os
import csv
VoterID = []
County = []
Candidate = []
Candidate2 = []
candidate_list = []
candidate_vote = []
candidate_percent = []
# Set path for file
csvfile = os.path.join("Resources", "election_data.csv")
# Open and read csv
# Read through each row of data after the header
with open(csvfile, mode="r") as csvfile:
csvreader = csv.reader(csvfile, delimiter=",")
for row in csvreader:
VoterID.append(row[0])
County.append(row[1])
Candidate.append(row[2])
Candidate2 = Candidate.copy()
#Request 1) The total number of votes cast
total_votes = len(VoterID) - 1
#Request 2) A complete list of candidates who received votes
# traverse for all elements
for Candidate in Candidate[1:]:
# check if exists in unique_list or not
if str(Candidate) not in candidate_list:
candidate_list.append(Candidate)
#Request 4) The total number of votes each candidate won
for row in candidate_list[0:]:
irow = (candidate_list.index(row))
vote_count = (Candidate2.count(str(candidate_list[irow])))
candidate_vote.append(vote_count)
#Request 3) The percentage of votes each candidate won
vote_percent = round((vote_count / total_votes)*100,4)
candidate_percent.append(vote_percent)
#Request 5) The winner of the election based on popular vote.
winner_vote = max(candidate_vote)
winner_row = candidate_vote.index(winner_vote)
winner = (str(candidate_list[winner_row]))
#================================================================================================
#================================================================================================
#Title
print("Election Results")
print(" ")
print("---------------------------")
print(" ")
#Request 1) The total number of votes cast
print("Total Votes: " + str(total_votes))
print(" ")
print("---------------------------")
#Request 2) #A complete list of candidates who received votes
#Request 3) The percentage of votes each candidate won
print(" ")
print(str(candidate_list[0]) + ": " + str(candidate_percent[0]) + "00% " + "(" + str(candidate_vote[0]) + ")")
print(str(candidate_list[1]) + ": " + str(candidate_percent[1]) + "00% " + "(" + str(candidate_vote[1]) + ")")
print(str(candidate_list[2]) + ": " + str(candidate_percent[2]) + "00% " + "(" + str(candidate_vote[2]) + ")")
print(str(candidate_list[3]) + ": " + str(candidate_percent[3]) + "00% " + "(" + str(candidate_vote[3]) + ")")
print(" ")
#Request 4) The winner of the election based on popular vote.
print("---------------------------")
print("Winner: " + winner)
print("---------------------------")
#================================================================================================
#================================================================================================
#Send to Text
myFile = open('pypoll_analysis.txt', 'w')
with myFile:
#Title
myFile.write("Election Results\n")
myFile.write(" \n")
myFile.write("---------------------------\n")
myFile.write(" \n")
#Request 1) The total number of votes cast
myFile.write("Total Votes: " + str(total_votes) + "\n")
myFile.write(" ")
myFile.write("---------------------------\n")
#Request 2) #A complete list of candidates who received votes
#Request 3) The percentage of votes each candidate won
myFile.write(" \n")
myFile.write(str(candidate_list[0]) + ": " + str(candidate_percent[0]) + "00% " + "(" + str(candidate_vote[0]) + ")\n")
myFile.write(str(candidate_list[1]) + ": " + str(candidate_percent[1]) + "00% " + "(" + str(candidate_vote[1]) + ")\n")
myFile.write(str(candidate_list[2]) + ": " + str(candidate_percent[2]) + "00% " + "(" + str(candidate_vote[2]) + ")\n")
myFile.write(str(candidate_list[3]) + ": " + str(candidate_percent[3]) + "00% " + "(" + str(candidate_vote[3]) + ")\n")
myFile.write(" \n")
#Request 4) The winner of the election based on popular vote.
myFile.write("---------------------------\n")
myFile.write("Winner: " + winner + "\n")
myFile.write("---------------------------\n")