-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
71 lines (55 loc) · 1.67 KB
/
main.py
1
#Import modulesimport osimport csvcsvPath = os.path.join('Resources/election_data.csv')txtPath = os.path.join('Analysis/election_results.txt')#Define variablestotalVotes = 0candidateVotes = {}winner = Nonewith open(csvPath) as csvData: #CSV reader voteData = csv.DictReader(csvData) # print(voteData.fieldnames) for row in voteData: candidate = row['Candidate'] if candidate not in candidateVotes: candidateVotes[candidate] = 0 candidateVotes[candidate] = candidateVotes[candidate] + 1 totalVotes = totalVotes + 1# print(totalVotes)# print(candidateVotes)# Locate the winner in the candidateVotes dictionaryfor candidate in candidateVotes: # print(candidate) votes = candidateVotes[candidate] if winner is None or votes > candidateVotes[winner]: winner = candidate # print(winner) #Generating output of total votesoutput = f'''Election Results-------------------------Total Votes: {totalVotes}-------------------------''' # Khan: 63.000% (2218231) # Correy: 20.000% (704200) # Li: 14.000% (492940) # O'Tooley: 3.000% (105630) # ------------------------- # Winner: Khan # ------------------------- # ```#Generate list of candidates and vote information for candidate in candidateVotes: votes = candidateVotes[candidate] percentage = votes / totalVotes output += candidate+": "+"{:.3f}".format(percentage*100.)+"% ("+str(votes)+")\n" output += f'''-------------------------Winner: {winner}-------------------------'''print(output)with open(txtPath,"w") as txtFile: txtFile.write (output)