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.
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
- Read the value once, before the loop. (The paper has done this for you.)
whilethe value is bad:- 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
andwhere it should beor. See above. This is the classic slip.position <= 1 or position >= 100. "Inclusive" means 1 and 100 are allowed. This version rejects them.ifand notwhile. Anifasks again once. A user who is wrong twice gets through.- No input inside the loop. Then
positionnever changes and the loop never ends. - A
forloop. 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.
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)
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
- Question 7: Ask for an email address twice and check that they match 5 marks
- Question 8: A bonus payment from items sold and years employed 7 marks
- Question 13.2: Find a run of five in a sorted array of 100 cards 6 marks
- Question 14.2: Write a subroutine that counts the asterisks on a bingo ticket 8 marks
Every AQA 8525 question we have worked
Learn it step by step
- F6.1 Defensive design and validation Robust programs
- F2.6 Condition-controlled loops: while Decisions and loops
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.