Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """A program is required to register user votes for a talent contest involving 5
- candidates. The user will select the candidate they wish to vote for in order to
- register their vote.
- When all the votes have been cast the program should sort the candidates
- into order based on the number of votes they have received. The result will
- then be output to the screen.
- Your program should display the list of candidates and prompt the user to
- vote for a candidate. The program should continue prompting for a vote until -
- 1 is entered.
- After each vote is cast your program should redisplay the candidate list.
- Write, run and test your program with each candidate receiving at least 1 vote
- and a clear winner being produced. Take a screen shot of your tested output
- called tally_chart.jpg.
- Portfolio The task contribution to your portfolio is:
- • The Python source code file for the task
- • tally_chart.jpg showing output from tally_chart.py when tested."""
- def print_candidates():
- print("vote for candidate by entering corresonding number\nenter -1 to exit")
- for i in range(len(candidates)):
- print(f"{i+1} - {candidates[i]} - {votes[i]}")
- def sort_candidates():
- sorted = False
- while not sorted:
- sorted = True
- for i in range(len(candidates)):
- for j in range(len(candidates)-1):
- if votes[j] < votes[j+1]:
- temp_vote = votes[j]
- votes[j] = votes[j+1]
- votes[j+1] = temp_vote
- temp_candidate = candidates[j]
- candidates[j] = candidates[j+1]
- candidates[j+1] = temp_candidate
- sorted = False
- candidates = ["John Smith", "John Rogers", "Jack Jones", "Jack Smith", "Bill Bong"]
- votes = []
- for i in range(len(candidates)):
- votes.append(0)
- vote = 0
- finished = False
- while not finished:
- # print("vote for candidate by entering corresonding number\nenter -1 to exit")
- # for i in range(len(candidates)):
- # print(f"{i+1} - {candidates[i]} - {votes[i]}")
- print_candidates()
- vote = input("enter your vote: ")
- if vote.isdigit():
- vote = int(vote)
- if vote < 6 and vote > 0:
- votes[vote-1] +=1
- print(f"You voted for {candidates[vote-1]}")
- else:
- print("range error")
- elif vote == '-1':
- finished = True
- sort_candidates()
- print_candidates()
- print(f"Goodbye")
- else:
- print("Only use integers")
Advertisement
Add Comment
Please, Sign In to add comment