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.
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."