its python program The college has several departments. For the purpose of the e
ID: 3816576 • Letter: I
Question
its python program
The college has several departments. For the purpose of the election, each department on each campus manages its own voting process and directly reports the votes received by each candidate to the election committee. Therefore, the same candidate name is received with different number of votes from different departments.
Input
Keyboard input of candidate-name, department, number-of-votes sets for the candidates.
Output
Election results in a tabular form and the winner.
The election committee wants the output to look as follows (actual names and numbers will depend on the input … the data in this table is only an example)
--------------Election Results--------------
Candidate Name Votes Total
-------------- -----------
Alice in Wonderland 1,065
Balto 272
Donald Duck 51
Minnie Mouse 349
Pluto 0
Tiger 1,242
Winnie the Pooh 548
Winner: ???, Votes Received by winner: ????
Total votes polled: ????
Processing
Create a two-dimensional list for votes. Each entry in the list represents the data that is entered for each candidate so that each entry consists of voting results in the following form:
candidateName department numberOfVotesForThisCandidate
For example, the votes list may look like:
[[Donald, Physics, 23],
[Pluto, Nursing, 56],
[Doc, Physics, 25],
[Pluto, Computer Science, 23],
[Tiger, English, 725],
.
.
]
The first line indicates that Donald received 23 votes from Physics department. The last two dots just mean that there may be more data rows.
The first task to be performed by the program is to read input data into the list of voting data.
The program will then add up votes and display results to the screen as shown in the Output sub-section. HINT: you may create a summaries list for the totals.
Explanation / Answer
# pastebin link for code: https://pastebin.com/se69jew9
def getOneCandidateData():
data = []
name = input("Enter candidate name: ")
department = input("Enter department: ")
votes = int(input("Enter votes: "))
return [name, department, votes]
def getVotingdata():
votes = []
while True:
try:
moreVotes = int(input("Add voting data (1. Yes, 2. No?): "))
if (moreVotes != 1 and moreVotes != 2):
print(moreVotes)
print("Enter 1 or 2 only")
continue
elif moreVotes == 1:
votes.append(getOneCandidateData())
else:
return votes
except:
print("Enter 1 or 2 only")
continue
def getElectionResults(voting_data):
voting_count = {}
for data in voting_data:
if data[0] in voting_count:
voting_count[data[0]] += int(data[2])
else:
voting_count[data[0]] = int(data[2])
return voting_count
def getWinner(voting_count):
maxVotes = -1
winner = ""
for candidate in voting_count:
if voting_count[candidate] > maxVotes:
maxVotes = voting_count[candidate]
winner = candidate
return winner
def printElectionResults(voting_count):
winner = getWinner(voting_count)
print(winner)
print("--------------Election Results--------------")
print("Candidate Name Votes Total")
print("-------------- -----------")
for candidate in voting_count:
print(candidate, " ", "{:,}".format(voting_count[candidate]))
print("Winner: ", winner, " Votes Received by winner: ", "{:,}".format(voting_count[winner]))
voting_data = getVotingdata()
print(voting_data)
voting_count = getElectionResults(voting_data)
printElectionResults(voting_count)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.