Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function named class_presidents that tallies votes for presidents of two

ID: 3596508 • Letter: W

Question

Write a function named class_presidents that tallies votes for presidents of two school classes. Somehow the vote tallies for sophomore and junior class presidents got mixed up! Your function should accept as its parameter a string representing a ilename of vote results. It should output the next sophomore and junior class presidents. The file format looks like the following: Jared s 25 Sophie j 12 Tom j 44 Isaac s 30 Emily s 68 Russ s 23 Madison j 20 The file repeats the pattern name year votes. The year is either "s" for sophomore or "j" for junior. Your program should output the candidate in eackh presidential race with the most votes, and how many votes they got. Specifically, if this file were named candidates.txt, your function should produce the following output: Sophomore Class President: Emily (60 votes) Junior Class President: Tom (44 votes) You may assume that there are no ties and that each class has one single person who received the most votes. Type your Python solution code here:

Explanation / Answer

def class_presidents(filename):
votesStr = ""
with open(filename) as fh:
votesStr = fh.readlines()[0]

votesList = votesStr.split()

maxSVotes = 0
winnerSName = 0

maxJVotes = 0
winnerJName = 0
for i in range(0, len(votesList), 3):
(name, year, votes) = (votesList[i], votesList[i+1], int(votesList[i+2]))
if year == 'j':
if maxJVotes < votes:
maxJVotes = votes
winnerJName = name
elif year == 's':
if maxSVotes < votes:
maxSVotes = votes
winnerSName = name

print("Sophomore Class President: " + winnerSName + " (" + str(maxSVotes) + " votes)")
print("Junior Class President: " + winnerJName + " (" + str(maxJVotes) + " votes)")

# copy pastable code link: https://paste.ee/p/UZnwz

Sample run

Sophomore Class President: Emily (60 votes)
Junior Class President: Tom (44 votes)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote