Counting machine for ballots cast.
★☆☆A game show allows contestants to ask the audience to help them choose a correct answer to a question from four possible answers. Audience members each have a small hand-held terminal on which they can press one of four buttons labelled A, B, C or D. As the audience vote, their selection is appended to a list. Once the time for voting has ended the results are collated and displayed to the contestant.
Write a program to output the total number of votes cast for each answer. The votes cast were: “A”, “B”, “B”, “B”, “B”, “C”, “C”, “D”, “A”, “B”, “A”, “B”, “A”, “B”, “D”, “B”, “C”, “B”, “B”, “A”.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
count_votes that:ballot that is the list of the votes cast.count_votes subprogram is called.Answer A: 5
Answer B: 10
Answer C: 3
Answer D: 2
# Voting count program
# -------------------------
# Subprograms
# -------------------------
# Function to count votes A, B and return results as an array
---
def count_votes(ballot):
---
result = [0, 0, 0, 0]
---
# Look at each vote cast
for vote in ballot:
---
# Count A, B, C, D
match vote:
---
case "A":
---
result[0] = result[0] + 1
---
case "B":
---
result[1] = result[1] + 1
---
case "C":
---
result[2] = result[2] + 1
---
case "D":
---
result[3] = result[3] + 1
---
return result
---
# -------------------------
# Main program
# -------------------------
---
votes = ["A", "B", "B", "B", "B", "C", "C", "D", "A", "B",
"A", "B", "A", "B", "D", "B", "C", "B", "B", "A"]
---
result = count_votes(votes)
---
print("Answer A:", result[0])
print("Answer B:", result[1])
print("Answer C:", result[2])
print("Answer D:", result[3])