OCR GCSE Computer Science June 2023 Paper 2, Question 5(c): the adding game

OCR J277/02 June 2023, Question 5(c): write an algorithm for a game that asks three addition questions with random numbers from 1 to 10 and keeps a score. A model answer in OCR reference language and Python, the six marks explained, and the game to play.

Past paper questionOCR J277/02June 2023 Paper 26 marksWrite a program

Question 5(c) of the OCR GCSE Computer Science Paper 2 sat on 25 May 2023 (J277/02) is the biggest algorithm question in Section A: 6 marks for a short game. In Section A you can answer in pseudocode, OCR Exam Reference Language or Python. The marks are for the logic.

We do not copy the exam paper here. Open it beside this page: OCR June 2023 J277/02 question paper (PDF). When you have finished, check the mark scheme too.

The question in short

Write an algorithm for an adding game:

  • the player is asked 3 addition questions;
  • each question adds two random whole numbers from 1 to 10 inclusive;
  • a correct answer adds 1 to the score;
  • the score is displayed at the end.

Plan before you write

Each bullet is a mark, more or less. Ask where each thing happens: before the loop, inside it, or after it.

  • Before: set the score to 0.
  • Inside, three times: pick two random numbers, ask the question, check the answer, add to the score.
  • After: output the score.

A model answer

In OCR Exam Reference Language:

score = 0
for count = 1 to 3
    num1 = random(1, 10)
    num2 = random(1, 10)
    answer = int(input("What is " + str(num1) + " + " + str(num2) + "? "))
    if answer == num1 + num2 then
        score = score + 1
    endif
next count
print("You scored " + str(score))

In Python:

import random
score = 0
for count in range(3):
    num1 = random.randint(1, 10)
    num2 = random.randint(1, 10)
    answer = int(input("What is " + str(num1) + " + " + str(num2) + "? "))
    if answer == num1 + num2:
        score = score + 1
print("You scored", score)

Where the six marks are

One mark each for:

  • setting the score to 0 before it is used, outside the loop;
  • generating two random numbers from 1 to 10;
  • inputting the player's answer, with a question that shows the two numbers;
  • checking whether the answer is correct;
  • adding 1 to the score if it is;
  • repeating those steps three times;
  • outputting the score at the end.

That is seven points for six marks, so you can drop one and still get full marks. The mark scheme does not need you to cast the input to an integer, and it does not punish an off by one in the loop or the random range.

Where the marks are lost

  • Picking the random numbers before the loop. Then all three questions are the same. The mark scheme refuses the repetition mark if the same numbers are used every time.
  • score = 0 inside the loop. The score is wiped on every question.
  • Not showing the numbers. The player has to see the question. A bare input() does not ask anything.
  • Outputting the score inside the loop only. It must be displayed at the end.
  • Writing the code out three times with no loop. This does get the repetition mark, but it takes three times as long and three times the chances to slip.

Run it

Play it. The robot's light is green for a right answer and red for a wrong one, and at the end it drives 10 cm for each point.

Three questions, different every time you run it. Get one wrong on purpose and check that the score is right.
The program
from bugbot import *
import random
connect()

score = 0
for count in range(3):
    num1 = random.randint(1, 10)
    num2 = random.randint(1, 10)
    answer = int(input("What is " + str(num1) + " + " + str(num2) + "? "))
    if answer == num1 + num2:
        led("green")
        score = score + 1
    else:
        led("red")
print("You scored", score)
forward(60, distance=score * 10)
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Parts (a) and (b) of the same question

The question also asks about testing and validation for this game. For validation, two methods that fit are a type check (the answer must be a whole number, not text) and a range check (two numbers from 1 to 10 cannot add up to less than 2 or more than 20). Each method needs a sentence on how it would be used in this game for the full marks.

Questions

What is the answer to OCR J277 June 2023 Paper 2 Question 5(c)?

Set score to 0. Loop three times: generate two random numbers from 1 to 10, ask the player for their sum, and add 1 to score if the answer equals num1 + num2. After the loop, output the score.

How do you generate a random number in OCR Exam Reference Language?

Use random(1, 10), which gives a random whole number from 1 to 10 with both ends included. In Python the same thing is random.randint(1, 10) after import random.

Can I answer OCR Paper 2 Section A in Python?

Yes. In Section A an algorithm can be written in pseudocode, OCR Exam Reference Language or a high-level language. In Section B, questions that say so must be answered in OCR Exam Reference Language or a high-level language such as Python.

More from this paper

Every OCR J277 question we have worked

Learn it step by step

  1. F3.7 Random numbers Strings, lists and records
  2. F2.5 Count-controlled loops: for Decisions and loops
  3. F13.4 Programming questions Exam preparation
Open the lessons

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.