GCSE · Computer Science · Edexcel · Spec 1CP2
Validation – range check
Type your age as 'ten', then 150, then minus five. Does the computer stop you? Not on its own. Let's build the lines that do.
Computer Science · Validation
Follow one user through the loop
The junior team only takes ages 6 to 13, both included. A crash is when a program suddenly stops because of an error it cannot recover from, so watch how each entry avoids one.
Three entries are coming: ten, 15, then 13. At each one, ask: did try/except, the if test or the break deal with it?
1 while True:2 try:3 age = int(input("Enter your age: "))4 if age >= 6 and age <= 13:5 break6 else:7 print("You must be between age 6 and 13 to join")8 except ValueError:9 print("Please enter a whole number")10 print("Age accepted")
| typed | age | test | route |
|---|---|---|---|
| loop starts |
Output
Step 1: while True: the condition is always True, so this loop never stops by itself. Only a break inside it can end it.
Press Next and watch the highlighted line. The page does not run the code: each step is written out for you.
Computer Science · Validation
Which check catches it?
Place each entry under the check that catches it, or under "Passes" if no check would reject it.
Still to sort
Empty string check (0)
Makes sure the user has entered something.
Where the line is: It only asks whether anything was typed, not how long it is or what it is worth.
Minimum length check (0)
Makes sure the input meets a minimum character requirement.
Where the line is: It counts characters. It does not compare a number against limits.
Range check (0)
Makes sure a numerical input falls within a set range.
Where the line is: It is about the size of a number, not how many characters it has.
Passes (0)
No check rejects it.
Where the line is: Valid for the rule that was set.
Programs need to cater for all likely inputs. Predict what a user might type, then decide which check would deal with it.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
How a program refuses bad data and asks again, because you built it to
What you need to know
- Validation means checking data is acceptable, against a set of criteria, before the program uses it.
- Validation ensures data is reasonable, helps prevent crashes, helps keep data accurate and reliable, and improves the user experience.
- A range check confirms numerical input falls within a set range, like an age between 13 and 100.
Have a goA classmate says: "My age check is fine with 150, because 150 is a number." Is she right?
No. 150 is a number, but it is outside the range, and an age like that is not sensible.
A range check tests whether the value falls within the set range, not just whether it is numerical.
- Programs only ask again if they are programmed to. Otherwise they run on with invalid data and may crash.
- Programs need to cater for all likely inputs, so predict what users could type and check for it.
- Asked for a number between one and 10, 12 and -5 are out of range, but 7 is correct.
Have a goSame prompt: enter a number between one and 10. Which of 0, 9 and 11 would the range check reject?
0 and 11.
0 is below the range and 11 is above it, so both are out of range. 9 sits inside it.
- Three validation checks make code more robust: empty string check, minimum length check and range check.
- Inclusive means both end values are allowed: for ages 6 to 13, accept only when age >= 6 and age <= 13.
Have a goAges 6 to 13 inclusive. Someone types 13. Is age <= 13 True or False, and does 13 get in?
True, so 13 gets in.
The equals part of <= allows the end value itself, which is what inclusive means.
- A while True loop keeps asking, an if tests the range, and break ends the loop once the input is valid.
- A try and except block catches a ValueError when a non-number is typed, shows a message, and the question is asked again.
The big picture
Validation checks that data is acceptable before a program uses it. A range check does this for numbers, and a while True loop with try/except, an if test and a break keeps asking until valid data is entered.
Key points
Worked example
Problem
Work out whether the condition age >= 6 and age <= 13 is True or False for each of these ages: 5, 6, 13 and 14.
⚠ Watch out
Writing > and < when both end values should be allowed. For ages 6 to 13 inclusive, that turns away a 6-year-old and a 13-year-old.
Memory hook
Ask, Catch, Check, Break: while True asks again, except catches the word, the if checks the limits, break lets valid data out.
Check yourself
Your friend says: "Python will ask again by itself if I type 150 for my age." In two sentences, correct them. Use the words loop and range check.
Flashcards
(12)What is validation?
Give the four purposes of validation.
What is a crash?
What does a range check do?
What does inclusive mean for a range check?
Why is a range check put inside a loop?
Which statement ends a range check loop?
What goes in the try block of a range check?
What happens when a ValueError occurs in the try block?
Name the three validation checks.
What does an empty string check ensure?
What is robust code?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore Edexcel GCSE Computer Science topics
- Programming arithmetic operators
- Programming logical operators
- Programming relational operators
- Reading, writing, analysing and refining programs
- Single entry/exit code blocks
- String manipulation
- Structural components of programs
- Structured data types
- User input handling
- Validation – length check
- Validation – presence check
- Variables and constants in programs
How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 21 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.