Edexcel GCSE Computer Science June 2023 Paper 2, Question 2: fixing the exercise program

Pearson Edexcel 1CP2/02 June 2023, Question 2: fix two syntax errors, a NameError, an IndexError and a logic error, and complete a random.randint call, in a Python program that picks warm-up exercises from a list. Each fix explained, with the working program to run.

Past paper questionPearson 1CP2/02June 2023 Paper 28 marksFix the code

Question 2 of the Pearson Edexcel GCSE Computer Science Paper 2 sat on 25 May 2023 (1CP2/02, Application of Computational Thinking) is an 8 mark debugging question. The paper tells you which lines are wrong and what kind of error each one is. Knowing what each error name means gets you most of the way.

We do not copy the exam paper or Pearson's code files here. Open the paper beside this page: Edexcel June 2023 Paper 2 question paper (PDF). When you have finished, check the mark scheme too.

The question in short

A program stores five warm-up exercises in a list. It prints them all, asks the user for a number, then picks that many exercises at random and prints them.

The seven fixes

The problem The fix Why
Syntax error: the import misspells the library import random a library name must be exact
One exercise name in the list has no quotation marks "pushups" without quotes Python looks for a variable called pushups
Syntax error: the for line has no colon for exercise in exerciseTable: every for, while, if and def line ends in a colon
The random call is unfinished random.randint(0, 4) five items have the index numbers 0 to 4, and randint includes both ends
IndexError: exerciseTable[index + 1] exerciseTable[index] when index is 4, index + 1 is 5, which is outside the list
NameError: the print misspells the variable print(name) the variable is called name
Logic error: one exercise too few range(numExercises) range(n) already runs n times. Taking 1 off loses one

The eighth mark is for adding white space, such as a blank line between sections, to make the code easier to read.

What the error names mean

  • SyntaxError: the line breaks Python's grammar. The program will not start.
  • NameError: a name is used that has never been given a value. Usually a spelling slip, or missing quotation marks.
  • IndexError: an index number is outside the list.
  • Logic error: no message at all. The program runs and does the wrong thing.

Where the marks are lost

  • random.randint(1, 5). That can give 5, which is outside the list, and can never give 0, so squats is never chosen.
  • random.randint(0, 5). randint includes its top number.
  • Fixing the IndexError with index - 1. Then an index of 0 becomes -1, which in Python is the last item. No crash, and the wrong answer.
  • Changing lines you were not asked to change. Fix what is listed, run it, and save it under the new name.

Run it

The corrected program. Ask for 3 exercises.

It lists the five exercises, then prints as many random ones as you ask for. Ask for 3 and count them.
The program
from bugbot import *
import random
connect()

exerciseTable = ["squats", "planks", "pushups", "lunges", "burpees"]

for exercise in exerciseTable:
    print(exercise)

numExercises = int(input("How many exercises? "))

for count in range(numExercises):
    index = random.randint(0, 4)
    name = exerciseTable[index]
    print(count + 1, name)
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.

Questions

What causes a NameError in Python?

Using a name that Python does not know: a misspelt variable, a variable used before it has a value, or a string written without quotation marks.

What causes an IndexError in Python?

Asking for a position that is outside the list. A list of five items has the positions 0 to 4, so position 5 does not exist.

What is the difference between random.randint and random.randrange?

random.randint(0, 4) can return 0, 1, 2, 3 or 4: both ends are included. random.randrange(0, 4) stops before 4.

More from this paper

Every Edexcel 1CP2 question we have worked

Learn it step by step

  1. F1.4 Errors: syntax, runtime and logic Programming basics
  2. F6.4 Debugging logic errors Robust programs
  3. F3.7 Random numbers Strings, lists and records
Open the lessons

This is our own explanation of a published exam question. It is not written or endorsed by Pearson, and the question paper and mark scheme remain Pearson's copyright. Read them on Pearson's site with the links on this page.