Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

PyPoll

Code

totalVoters = 0
khan = 0
correy = 0
li = 0
o =  0
with open('election_data.csv', newline ='') as csvfile:
    data = csv.reader(csvfile, delimiter = ',')
    next(data)
    for row in data:
        totalVoters += 1
        if row[2] == "Khan":
            khan += 1
        elif row[2] == "Correy":
            correy += 1
        elif row[2] == "Li":
            li += 1
        elif row[2] == "O'Tooley":
            o += 1

dic = {"Khan":khan,"Correy":correy,"Li":li,"O'Tooley":o}
winner = max(dic, key=dic.get)

answer = ("Election Results"+
    "\n------------------------"+
    "\nTotal Votes:" + str(totalVoters)+
    "\n------------------------"+
    "\nKhan: " + str(round((khan/totalVoters)*100))+"% "+str(khan)+
    "\nCorrey: " + str(round((correy/totalVoters)*100))+"% "+str(correy)+
    "\nLi: " + str(round((li/totalVoters)*100))+"% "+str(li)+
    "\nO'Tooley: " + str(round((o/totalVoters)*100))+"% "+str(o)+
    "\n------------------------"+
    "\nWinner:"+ winner)

print(answer) 
with open('Answer.csv', 'w', newline='\n') as f:
    f.write(answer)

Answer

Election Results


Total Votes:3521001


Khan: 63% 2218231

Correy: 20% 704200

Li: 14% 492940

O'Tooley: 3% 105630


Winner:Khan