OCR town are holding an election with three candidates (A, B and C) - OCR - GCSE Computer Science - Question 8 - 2018 - Paper 1
Question 8
OCR town are holding an election with three candidates (A, B and C). An electronic voting booth will be used to allow people to vote.
Write an algorithm that:
- Al... show full transcript
Worked Solution & Example Answer:OCR town are holding an election with three candidates (A, B and C) - OCR - GCSE Computer Science - Question 8 - 2018 - Paper 1
Step 1
Initialisation of A, B and C as zero
96%
114 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
Initialize variables:
countA = 0
countB = 0
countC = 0
totalVotes = 0
Step 2
Allows input of anything from the user
99%
104 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
Create a loop that will continue until "END" is entered:
while True:
vote = input('Enter A, B or C (or type "END"): ')
Step 3
Incrementing A, B and C depending on input
96%
101 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
Process the input and update the vote counts:
if vote == 'A':
countA += 1
totalVotes += 1
elif vote == 'B':
countB += 1
totalVotes += 1
elif vote == 'C':
countC += 1
totalVotes += 1
elif vote == 'END':
break
else:
print('Invalid input, please enter A, B or C')
Step 4
At any point allows the official to type in 'END'
98%
120 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
Print the results when 'END' is entered:
print(f'A: {countA}, B: {countB}, C: {countC}, Total Votes: {totalVotes}')