Question: Write a function vote () that takes as a parameter a list of names of candidates up for election and repeatedly asks the user to

Write a function vote() that takes as a parameter a list of names of candidates up for election and repeatedly asks the user to enter a name of one of the candidates. When the user enters the empty string, the function prints for every name on the ballot the number of votes that the candidate received. The candidates and vote totals can be printed in any order as long as all of the information is presented.If the user types a name that cannot be found in the list of candidates, the vote is recorded for the 'Unknown' candidate.

Capitalization shouldn't matter so that a candidate's name appearing in all uppercase, all lowercase, or in mixed case should record a vote for the associated person. When the user types a blank name, then the function should stop and report the vote totals. The vote totals should be reported using correct grammar, meaning using was/were and the singular or plural of vote appropriately. The following shows a sample run of the vote() function. Please note that your function must work on all valid examples, not just the ones provided below:

Write a function vote() that takes as a parameter a list of

Can someone solve this without the use of [i.lower() for i in candidate_list] and for k,v in vote_count.items():because my instructor says that it's not allowed because these loops weren't taught to us yet. Also, indent your code.

This is my original code:

def vote(candidate_list): 'insert a list of names' vote_count={} #set an empty dictionary vote_count['unknown']=0 #Have a counter for unknown for name in candidate_list: #Each name will start at zero vote_count[name]=0 t_list=vote_count.keys() t_list=[i.lower() for i in candidate_list] while True: name = input("Enter a vote: ") #Ask for the name if name=="": #if it's blank then stop break if name.lower() not in t_list: #Count towards that are not in dictionary vote_count['unknown']+=1 else: for cand_name in candidate_list: #increase the number if matches if name.lower() == cand_name.lower(): vote_count[cand_name]+=1 for k,v in vote_count.items(): #arrange in a way that it orgnaize on how many got the votes if v == 1: print("There was " + str(v) +" votes for "+ k+".") if v > 1: print("There were " + str(v) +" votes for "+ k+".") if v == 0: print("There were " + str(v) +" votes for "+ k+".")

Python 3.7.0 Shell File Edit Shell Debug Options Window Help Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD6 4)] on win32 Type "copyright", "credits" or "license" for more information RESTART: C:/Users/eve/Desktop/hw8.py >>>vote(['Rauner', 'Pritzker', "Jackson']) Enter a vote: Rauner Enter a vote: Rauner Enter a vote: Rauner Enter a vote: pritzKER Enter a vote: Pritzker Enter a vote Dave Enter a vote: Pete Enter a vote: There were 3 votes for Rauner. There were 2 votes for Pritzker There were 0 votes for Jackson There were 2 votes for Unknown >>>vote ('Mouse', Duck, Hook,Smee']) Enter a vote: mouse Enter a vote: mouse Enter a vote: duck Enter a vote: DUCK Enter a vote: hoOk Enter a vote: White Enter a vote: Hare Enter a vote: There were 2 votes for Mouse There were 2 votes for Duck. There was 1 votes for Hook There were 0votes for Smee. There were 2 votes for Unknown Ln: 32 Col: 4

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!