Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is:

Respuesta :

Answer:

a= input("Enter the last name of first candidate: ")

aVotes= int(input("Enter Number of votes cast for "+ a + ": "))

b= input("Enter the last name of second candidate: ")

bVotes= int(input("Enter Number of votes cast for "+ b + ": "))

c= input("Enter the last name of third candidate: ")

cVotes= int(input("Enter Number of votes cast for "+ c + ": "))

d= input("Enter the last name of fourth candidate: ")

dVotes = int(input("Enter Number of votes cast for "+ d + ": "))

e= input("Enter the last name of fifth candidate: ")

eVotes = int(input("Enter Number of votes cast for "+ e + ": "))

votes= [aVotes,bVotes,cVotes,dVotes,eVotes]

aPercent= (round((aVotes/sum(votes))*100, 2))

bPercent= (round((bVotes/sum(votes))*100, 2))

cPercent= (round((cVotes/sum(votes))*100, 2))

dPercent= (round((dVotes/sum(votes))*100, 2))

ePercent= (round((eVotes/sum(votes))*100, 2))

print( a, ":", aVotes,"Votes", aPercent,"%")

print( b, ":", bVotes,"Votes", bPercent,"%")

print( c, ":", cVotes,"Votes", cPercent,"%")

print( d, ":", dVotes,"Votes", dPercent,"%")

print( e, ":", eVotes,"Votes", ePercent,"%")

winner= max(votes)

if winner==aVotes:

   print(a," is the Winner")

elif winner==bVotes:

   print(b," is the Winner")

elif winner==cVotes:

   print(c," is the Winner")

elif winner==dVotes:

   print(d," is the Winner")

elif winner==eVotes:

   print(e," is the Winner")

else:

   None

Explanation:

  • First section

a= input("Enter the last name of first candidate: ")

aVotes= int(input("Enter Number of votes cast for "+ a + ": "))

b= input("Enter the last name of second candidate: ")

bVotes= int(input("Enter Number of votes cast for "+ b + ": "))

c= input("Enter the last name of third candidate: ")

cVotes= int(input("Enter Number of votes cast for "+ c + ": "))

d= input("Enter the last name of fourth candidate: ")

dVotes = int(input("Enter Number of votes cast for "+ d + ": "))

e= input("Enter the last name of fifth candidate: ")

eVotes = int(input("Enter Number of votes cast for "+ e + ": "))

Collects the last name and the number of  votes cast

  • Second section

votes= [aVotes,bVotes,cVotes,dVotes,eVotes]

places all the votes in a list.

The reason for this is to allow us use the "sum function" and "max function", to get the sum of all the votes.

  • Third section

aPercent= (round((aVotes/sum(votes))*100, 2))

bPercent= (round((bVotes/sum(votes))*100, 2))

cPercent= (round((cVotes/sum(votes))*100, 2))

dPercent= (round((dVotes/sum(votes))*100, 2))

ePercent= (round((eVotes/sum(votes))*100, 2))

calculates the percentage of all the votes cast for the individual candidates

  • Fourth section

print( a, ":", aVotes,"Votes", aPercent,"%")

print( b, ":", bVotes,"Votes", bPercent,"%")

print( c, ":", cVotes,"Votes", cPercent,"%")

print( d, ":", dVotes,"Votes", dPercent,"%")

print( e, ":", eVotes,"Votes", ePercent,"%")

prints out the candidates name, number of votes received and the percentage

  • Fifth section

winner= max(votes)

gets the maximum votes cast

  • Sixth Section

if winner==aVotes:

   print(a," is the Winner")

elif winner==bVotes:

   print(b," is the Winner")

elif winner==cVotes:

   print(c," is the Winner")

elif winner==dVotes:

   print(d," is the Winner")

elif winner==eVotes:

   print(e," is the Winner")

else:

   None

uses if statements to compare the maximum votes with the votes gotten by all the candidates to get a winner

Ver imagen jehonor
ACCESS MORE
EDU ACCESS