AQA GCSE Computer Science June 2024 Paper 1, Question 15: the row of cells game

AQA 8525 June 2024 Paper 1, Question 15: the 8 mark program. Extend a while loop so that a player moves 1 or 2 cells along a list, goes back to 0 on an X or past the end, and finishes at the last cell. A model answer, the index trap, and a robot that plays it.

Past paper questionAQA 8525/1BJune 2024 Paper 18 marksWrite a program

Question 15 is the last question of the AQA GCSE Computer Science Paper 1 sat on 15 May 2024 (8525/1B, the Python paper): 8 marks for the inside of a while loop. The rules fill two pages of the paper. The answer is eight lines.

We do not copy the exam paper here. Open it beside this page: AQA June 2024 Paper 1B question paper (PDF). When you have finished, check the mark scheme too.

The question in short

A list called row stands for a row of cells. Some cells contain an X. The player starts at position 0 and wants to reach the last cell.

  • On each turn the player enters 1 or 2, and moves forward by that many cells.
  • If the new position is past the end of the row, or contains an X, the program displays Bad move and the player goes back to position 0.
  • This repeats until the player reaches the end.

You are given three lines: pos = 0, then lastPos = len(row) - 1, then while pos < lastPos:. You write the body of the loop. The list can be any length, with Xs anywhere.

A model answer

pos = 0
lastPos = len(row) - 1
while pos < lastPos:
    move = int(input("Enter 1 or 2: "))
    if move == 1 or move == 2:
        pos = pos + move
        if pos > lastPos:
            print("Bad move")
            pos = 0
        elif row[pos] == "X":
            print("Bad move")
            pos = 0

The index trap

Check for "past the end" before you look in the list. If pos is 8 in a list of 8 cells, row[pos] does not exist, and the program crashes instead of saying Bad move.

In one condition it looks like this, and the order of the two halves matters for the same reason:

if pos > lastPos or row[pos] == "X":

Python stops reading an or as soon as the first half is true, so row[pos] is never looked at when pos is too big.

Where the eight marks are

Two marks are for design: a selection that outputs Bad move, and nested selection or more than one condition.

Six marks are for a program that works:

  • the move input inside the loop;
  • checking that the move is 1 or 2;
  • adding the move to pos, once for each turn;
  • going back to 0 if the move goes past the end;
  • going back to 0 if the cell contains an X;
  • using index numbers on row that never go out of range.

Any error caps you at 7. The mark scheme's own 7 mark example is one that forgets to check that the move is 1 or 2.

Where the marks are lost

  • The input outside the loop. One move is then repeated for ever.
  • row[pos + move] with no range check. See above.
  • pos == "X". pos is a number. The X is in the list: row[pos].
  • X with no quotation marks. It is a character, not a variable.
  • Resetting without the message, or the message without the reset. A bad move does both.
  • Hard-coding 7. The list can be any length. Use lastPos.

Run it

The game on the row from the paper's example, with Xs at positions 3 and 6. The robot is the player: it drives 6 cm for each cell, and reverses all the way back on a bad move.

The example from the paper: 1, then 2 (Bad move, back to 0), then 2, 2, 1 and 2 to finish at position 7.
The program
from bugbot import *
connect()

row = ["", "", "", "X", "", "", "X", ""]
CELL = 6

pos = 0
lastPos = len(row) - 1
while pos < lastPos:
    print("position", pos)
    move = int(input("Enter 1 or 2: "))
    if move == 1 or move == 2:
        here = pos
        pos = pos + move
        if pos > lastPos or row[pos] == "X":
            led("red")
            print("Bad move")
            if here > 0:
                backward(60, distance=here * CELL)
            pos = 0
        else:
            led("green")
            forward(60, distance=move * CELL)
print("position", pos, "- finished")
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 is the answer to AQA GCSE Computer Science 2024 Paper 1 Question 15?

Inside the loop, input the move as an integer. If it is 1 or 2, add it to pos. If pos is now greater than lastPos, or row[pos] is "X", display Bad move and set pos to 0.

How do I avoid an index out of range error?

Check that the index is inside the list before you use it. In a combined condition, put the range check first: if pos > lastPos or row[pos] == "X".

What is short circuit evaluation?

When the first half of an OR is true, or the first half of an AND is false, the answer is already known, so Python does not evaluate the second half. That is why putting the range check first protects the list lookup.

More from this paper

Every AQA 8525 question we have worked

Learn it step by step

  1. F3.3 Lists: one-dimensional arrays Strings, lists and records
  2. F2.6 Condition-controlled loops: while 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 AQA, and the question paper and mark scheme remain AQA's copyright. Read them on AQA's site with the links on this page.