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

I need to write a program tally() that takes a list of names of candiates up for

ID: 3644236 • Letter: I

Question

I need to write a program tally() that takes a list of names of candiates up for election and then asks the user to enter the name of one of the canidates repeatedly as a parameter. When the person using the program enters an empty string, the function will print for all the names that are on the ballot the number of votes that the canidate received. If the user types a name that doesn't exist in the list, then the vote is recorded for 'Other' canidate. CAPS also don't matter so however the user enters it should work. When a blank name is typed, then the function needs to stop and report all the totals of the vote count.


This is an example of how it should go.


>>> tally(['Harrison', 'Cruz', 'Patel') Enter a vote: Harrison Enter a vote: CRUZ Enter a vote: patel Enter a vote: PaTel Enter a vote: Haris Enter a vote: cruz Enter a vote: PATEL Enter a vote: Therewere 3 votes for Patel. Therewere 2 votes for Cruz. There was 1 vote for Harrison. There was 1 vote for Other.

Explanation / Answer

def tally(names):
    votes=[]
    lnames=[]
    for n in names:
        votes.append(0)
        lnames.append(n.lower())
    votes.append(0)

    done=False

    while not done:
        vote=raw_input("Enter a vote: ")
        vote=vote.lower()
        if vote=="":
            done=True
            continue
        if vote in lnames:
            place=lnames.index(vote)
            votes[place]=votes[place]+1
        else:
            votes[len(votes)-1]=votes[len(votes)-1]+1
   
    for i in range(len(names)):
        tense="was"
        vtense="vote"
        if votes[i]>1:
            tense="were"
            vtense="votes"
        print "There",tense,votes[i],vtense,"for",names[i]+"."
    tense ="was"
    vtense="vote"
    if votes[len(votes)-1]>1:
        tense="were"
        vtense="votes"
    print "There",tense,votes[len(votes)-1],vtense,"for","Other."