AQA GCSE Computer Science June 2023 Paper 1, Question 13: validating a grid reference

AQA 8525 June 2023 Paper 1, Question 13: extend a Python program so that it keeps asking until a valid grid reference such as C2 is entered. A model answer, the six marks explained, and a robot that drives to the square you type.

Past paper questionAQA 8525/1BJune 2023 Paper 16 marksWrite a program

Question 13 of the AQA GCSE Computer Science Paper 1 sat on 19 May 2023 (8525/1B, the Python paper) is a validation question. You are given the start of a Python program and asked to extend it. It is worth 6 marks.

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

The question in short

A game uses a 3 by 3 grid. The columns are A, B and C. The rows are 1, 2 and 3. A square is named by a letter and a number, such as C3.

A grid reference is valid if:

  • it has exactly two characters;
  • the first is A, B or C;
  • the second is 1, 2 or 3.

The code you are given already has a variable called check set to False, a loop that runs while check is False, and inside it a second loop that keeps asking until the text typed is two characters long. It also turns the text into capital letters.

You add the other two checks. Your code must use the variable check, keep asking until the reference is valid, and output a message when it is not.

Plan before you write

The given code has done the length check. So when your code starts, square has two characters in it. What is left:

  1. Take the two characters apart: square[0] and square[1].
  2. Test the first against A, B, C and the second against 1, 2, 3.
  3. If both pass, set check to True. That ends the outer loop.
  4. If not, print a message. The outer loop then goes round again by itself.

A model answer

The first six lines are the ones the paper gives you.

check = False
while check == False:
    square = ""
    while len(square) != 2:
        square = input("Enter grid reference (eg C2): ")
        square = square.upper()
    letter = square[0]
    number = square[1]
    if letter in "ABC" and number in "123":
        check = True
    else:
        print("Not a valid grid reference")

If you would rather not use in, write the conditions out in full:

    if (letter == "A" or letter == "B" or letter == "C") and (number == "1" or number == "2" or number == "3"):

Where the six marks are

Two marks are for design: using the variable check in your own code, and using selection to test the reference.

Four marks are for a program that works:

  • pulling the first and second characters out correctly (or comparing against all nine valid references);
  • at least one correct condition, such as letter == "A";
  • all the conditions correct, with check set properly in every case;
  • a message in a sensible place when the reference is not valid.

Any error in the code caps you at 5.

Where the marks are lost

  • if letter == "A" or "B" or "C". This does not mean what it says in English. Python reads it as (letter == "A") or "B" or "C", and the text "B" on its own counts as True. So every letter passes. Write the comparison out each time, or use in.
  • Comparing with numbers. square[1] is a character. number == 2 is never True. It must be number == "2".
  • or between the two tests. Both the letter and the number must be right, so the two halves are joined with and. With or, Z1 would pass.
  • Never setting check. Then the loop never ends. The question tells you to use it.
  • Indenting your code too far. If your if is inside the inner while, it runs before the length has been checked. It belongs at the same level as the inner while.

Run it

Here the grid is on the robot's mat, and a valid reference sends the robot to that square. The robot starts on B1.

Try D2, then B7, then 2B. Each is rejected. Then try c3: it is turned into C3, accepted, and the robot drives there.
The program
from bugbot import *
connect()

check = False
while check == False:
    square = ""
    while len(square) != 2:
        square = input("Enter grid reference (eg C2): ")
        square = square.upper()
    letter = square[0]
    number = square[1]
    if letter in "ABC" and number in "123":
        check = True
    else:
        led("red")
        print("Not a valid grid reference")

led("green")
print("Going to", square)
# columns A, B, C are 20 cm apart. So are rows 1, 2, 3
across = ("ABC".index(letter) - 1) * 20
up = (int(number) - 1) * 20
if across > 0:
    right(60, distance=across)
elif across < 0:
    left(60, distance=-across)
if up > 0:
    forward(60, distance=up)
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 2023 Paper 1 Question 13?

After the given length check, take the first and second characters of the input, test that the first is A, B or C and the second is 1, 2 or 3, set check to True if both are right, and otherwise print a message. The outer while loop repeats until check is True.

Why does "if letter == 'A' or 'B'" not work in Python?

Python evaluates letter == 'A' first and then treats 'B' as a separate condition. Any text that is not empty counts as True, so the whole condition is always True. Write letter == 'A' or letter == 'B', or use letter in 'AB'.

What is validation?

Validation is checking that data is sensible and allowed before the program uses it. Common checks are a length check, a range check, a type check and a presence check. This question uses a length check and then checks each character against a list of allowed values.

More from this paper

Every AQA 8525 question we have worked

Learn it step by step

  1. F6.1 Defensive design and validation Robust programs
  2. F3.1 String handling Strings, lists and records
  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.