AQA GCSE Computer Science June 2022 Paper 1, Question 13.1: the range check loop

AQA 8525 June 2022 Paper 1, Question 13.1: extend a Python program so that it keeps asking for a card position until it is between 1 and 100. The two line answer, the OR and AND trap, the four marks explained, and the loop to run.

Past paper questionAQA 8525/1BJune 2022 Paper 14 marksWrite a program

Question 13.1 of the AQA GCSE Computer Science June 2022 Paper 1 (8525/1B, the Python paper) asks for the most common piece of validation there is: keep asking until the number is in range. It is worth 4 marks and the answer is two lines long. Validation loops come up often on the programming paper, so learn this one as a pattern.

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

The question in short

You are given one line of Python, which reads a card position as an integer into a variable called position. You extend it so that the program keeps getting the user to enter the position until it is between 1 and 100 inclusive.

The pattern

  1. Read the value once, before the loop. (The paper has done this for you.)
  2. while the value is bad:
  3. read it again.

The loop condition describes the values you do not want. That is the part people get backwards.

A model answer

The first line is the one the paper gives you.

position = int(input("Enter card position: "))
while position < 1 or position > 100:
    position = int(input("Enter card position: "))

Why it is or

A position is bad if it is too small or too big. It cannot be both at once, so position < 1 and position > 100 is never true, and that loop would never run. Nothing would be checked.

If you would rather think about good values, wrap the good condition in not:

while not (position >= 1 and position <= 100):

Python also lets you write the good range as 1 <= position <= 100.

Where the four marks are

Two marks are for design: the idea of reading a number inside the loop, and using indefinite iteration (a while).

Two marks are for a program that works: one for a condition that checks one end of the range, and the second for checking both ends correctly.

Any error in the code caps you at 3.

Where the marks are lost

  • and where it should be or. See above. This is the classic slip.
  • position <= 1 or position >= 100. "Inclusive" means 1 and 100 are allowed. This version rejects them.
  • if and not while. An if asks again once. A user who is wrong twice gets through.
  • No input inside the loop. Then position never changes and the loop never ends.
  • A for loop. You cannot know how many tries the user will need.

Run it

Once the position is valid the robot uses it: it drives one tenth of the position, in centimetres. So 100 sends it 10 cm.

Try 0, then 101, then -5: each is refused. Try 100: it is accepted, because the range is inclusive, and the robot drives 10 cm.
The program
from bugbot import *
connect()

position = int(input("Enter card position: "))
while position < 1 or position > 100:
    led("red")
    position = int(input("Enter card position: "))
led("green")
print("Position", position, "accepted")
forward(60, distance=position / 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.

Questions

What is the answer to AQA GCSE Computer Science 2022 Paper 1 Question 13.1?

After the given input line, add while position < 1 or position > 100: and inside the loop read position again with int(input()).

Why is it OR and not AND in a range check loop?

The loop must repeat while the value is invalid. A value is invalid if it is below the range or above it. No number is both, so AND would never be true and the loop would never run.

What is a range check?

A range check is validation that makes sure a number lies between a lowest and a highest allowed value, such as 1 to 100.

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. F2.6 Condition-controlled loops: while Decisions and loops
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.