185k views
1 vote
Write a program that accepts the names of three political parties and the number of votes each received in the last mayoral election. Display the percentage of the vote each party received.

2 Answers

3 votes

Final answer:

To calculate the percentage of votes each political party received, input the votes, compute the total, then divide each party's votes by the total and multiply by 100.

Step-by-step explanation:

Programming Task: Calculating Election Percentages

To write a program that calculates the percentage of votes each political party received in an election, you need to gather the total number of votes and the number of votes each party secured. First, prompt the user for the names of the three political parties and the votes they each received. Then, calculate the total number of votes by summing up the votes of all parties. For each party, divide the number of votes it received by the total votes and multiply by 100 to get the percentage. Finally, display the results to the user.

This programming task involves basic arithmetic operations and is a good exercise to understand how to use variables, input/output operations, and perform calculations in a programming language.

User Shane LeBlanc
by
4.5k points
3 votes

Answer:

Written in Python

parties = []

votes = []

total = 0

for i in range(0,3):

party = input("Party: ")

vote = int(input("Vote: "))

parties.append(party)

votes.append(vote)

total = total + vote

for i in range(0,3):

print(parties[i]+"\t\t\t"+str(votes[i])+"\t\t\t"+str(round((votes[i])*100/total,2))+"%")

Step-by-step explanation:

This declares an empty list for parties

parties = []

This declares an empty list for votes

votes = []

This initializes total votes to 0

total = 0

The following iteration allows user to enter details for party and corresponding votes

for i in range(0,3):

party = input("Party: ")

vote = int(input("Vote: "))

parties.append(party)

votes.append(vote)

This calculates the total vote

total = total + vote

The following iteration prints the required details

for i in range(0,3):

print(parties[i]+"\t\t\t"+str(votes[i])+"\t\t\t"+str(round((votes[i])*100/total,2))+"%")

User JaPyR
by
4.8k points