OCR GCSE Computer Science June 2024 Paper 2, Question 9(f): finding the winning team
OCR J277/02 June 2024, Question 9(f): write an algorithm that takes team names and scores until the user enters stop, then outputs the team with the highest score. Model answers in OCR reference language and Python, the six marks explained, and the program to run.
Question 9(f) is the last question of the OCR GCSE Computer Science Paper 2 sat on 21 May 2024 (J277/02): 6 marks for an algorithm in OCR Exam Reference Language or a high-level language. It joins two patterns worth knowing by heart: loop until a sentinel value, and keep the highest so far.
We do not copy the exam paper here. Open it beside this page: OCR June 2024 J277/02 question paper (PDF). When you have finished, check the mark scheme too.
The question in short
Write an algorithm to:
- prompt the user for a team name and score, or "stop" to finish;
- keep taking names and scores until the user enters "stop";
- work out which team has the highest score;
- output the winning team's name and score in a message.
The two patterns
Loop until a sentinel. You do not know how many teams there are, so it is a while loop. Read the first name before the loop, and read the next name at the bottom of the loop. Then "stop" is never treated as a team.
Highest so far. Keep two variables: the best score seen, and the name that goes with it. Whenever a new score beats the best, replace both.
A model answer
In OCR Exam Reference Language:
highScore = -1
highTeam = ""
team = input("Enter a team name, or stop")
while team != "stop"
score = int(input("Enter the score"))
if score > highScore then
highScore = score
highTeam = team
endif
team = input("Enter a team name, or stop")
endwhile
print("The winner is " + highTeam + " with " + str(highScore) + " points")
In Python:
highScore = -1
highTeam = ""
team = input("Enter a team name, or stop: ")
while team != "stop":
score = int(input("Enter the score: "))
if score > highScore:
highScore = score
highTeam = team
team = input("Enter a team name, or stop: ")
print("The winner is", highTeam, "with", highScore, "points")
Where the six marks are
- inputting a team name and a score, stored separately;
- using iteration;
- repeating until "stop" is entered;
- working out the highest score;
- working out the winning team's name;
- outputting both.
The mark scheme is generous here. It does not need the variables to be initialised, it accepts break to leave the loop, it accepts lists with max, and it ignores ties.
Where the marks are lost
- Remembering the score and not the name.
highScorealone cannot tell you who won. The name must be saved at the same moment, inside the sameif. - A
forloop. The number of teams is not known. Without a loop that ends on "stop", two marks cannot be given. - Outputting inside the loop. The winner is only known once every team is in.
- Comparing text.
inputgives a string, and "9" is greater than "10" as text. Cast the score to an integer. if score > highScorewithhighScorestarting at 100. Start lower than any real score, such as -1.
Run it
Type team names and scores, then stop. The robot drives 1 cm for every 2 points of the winning score.
The program
from bugbot import *
connect()
highScore = -1
highTeam = ""
team = input("Enter a team name, or stop: ")
while team != "stop":
score = int(input("Enter the score: "))
if score > highScore:
highScore = score
highTeam = team
team = input("Enter a team name, or stop: ")
if highTeam == "":
print("No teams were entered")
else:
led("green")
print("The winner is", highTeam, "with", highScore, "points")
forward(60, distance=min(highScore / 2, 60))
Questions
What is the answer to OCR J277 June 2024 Paper 2 Question 9(f)?
Read a team name. While it is not "stop", read the score, and if it beats the highest so far, store the score and the team name. Read the next team name. After the loop, output the stored team name and score.
How do I find the largest value entered by a user?
Keep a variable for the largest so far, starting lower than any possible value. Compare each new value with it and replace it when the new value is bigger. If you need to know what the value belongs to, store that at the same time.
What is a sentinel value?
A special value, such as "stop" or -1, that the user enters to show that there is no more data. The loop repeats until it sees it.
More from this paper
- Question 8: Maintainability, and a function that moves a character and keeps it in range 10 marks
- Question 9(c): A range check that outputs VALID or NOT VALID, and its test data 7 marks
Every OCR J277 question we have worked
Learn it step by step
- F2.7 Loop patterns Decisions and loops
- F2.6 Condition-controlled loops: while Decisions and loops
- F13.4 Programming questions Exam preparation
This is our own explanation of a published exam question. It is not written or endorsed by OCR, and the question paper and mark scheme remain OCR's copyright. Read them on OCR's site with the links on this page.