KDT85

tally_chart.py

Nov 29th, 2022
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. """A program is required to register user votes for a talent contest involving 5
  2. candidates. The user will select the candidate they wish to vote for in order to
  3. register their vote.
  4. When all the votes have been cast the program should sort the candidates
  5. into order based on the number of votes they have received. The result will
  6. then be output to the screen.
  7. Your program should display the list of candidates and prompt the user to
  8. vote for a candidate. The program should continue prompting for a vote until -
  9. 1 is entered.
  10. After each vote is cast your program should redisplay the candidate list.
  11. Write, run and test your program with each candidate receiving at least 1 vote
  12. and a clear winner being produced. Take a screen shot of your tested output
  13. called tally_chart.jpg.
  14. Portfolio The task contribution to your portfolio is:
  15. • The Python source code file for the task
  16. • tally_chart.jpg showing output from tally_chart.py when tested."""
  17.  
  18. def print_candidates():
  19.     print("vote for candidate by entering corresonding number\nenter -1 to exit")
  20.     for i in range(len(candidates)):
  21.         print(f"{i+1} - {candidates[i]} - {votes[i]}")
  22.  
  23. def sort_candidates():
  24.     sorted = False
  25.     while not sorted:
  26.         sorted = True
  27.         for i in range(len(candidates)):
  28.             for j in range(len(candidates)-1):
  29.                 if votes[j] < votes[j+1]:
  30.                     temp_vote = votes[j]
  31.                     votes[j] = votes[j+1]
  32.                     votes[j+1] = temp_vote
  33.                     temp_candidate = candidates[j]
  34.                     candidates[j] = candidates[j+1]
  35.                     candidates[j+1] = temp_candidate
  36.                     sorted = False
  37.  
  38. candidates = ["John Smith", "John Rogers", "Jack Jones", "Jack Smith", "Bill Bong"]
  39. votes = []
  40. for i in range(len(candidates)):
  41.     votes.append(0)
  42. vote = 0
  43. finished = False
  44. while not finished:
  45.     # print("vote for candidate by entering corresonding number\nenter -1 to exit")
  46.     # for i in range(len(candidates)):
  47.     #     print(f"{i+1} - {candidates[i]} - {votes[i]}")
  48.     print_candidates()
  49.     vote = input("enter your vote: ")
  50.     if vote.isdigit():
  51.         vote = int(vote)
  52.         if vote < 6 and vote > 0:        
  53.             votes[vote-1] +=1
  54.             print(f"You voted for {candidates[vote-1]}")
  55.         else:
  56.             print("range error")
  57.     elif vote ==  '-1':
  58.             finished = True
  59.             sort_candidates()
  60.             print_candidates()
  61.             print(f"Goodbye")
  62.     else:
  63.         print("Only use integers")  
  64.  
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment