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 break
6 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")
typedagetestroute
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.

1 / 11

Press Next and watch the highlighted line. The page does not run the code: each step is written out for you.

Step 1 of 11: while True: the condition is always True, so this loop never stops by itself. Only a break inside it can end it..

Watch out: Nothing here checks anything by magic. Every refusal comes from a line somebody wrote.

Computer Science · Validation

Who is checking the age?

A program asks for the user's age. One user types 150. Another types -5. There is no range check anywhere in the code.

Which is closest to what you think happens next?
How sure are 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.

6 of 6 still to sort.

Programs need to cater for all likely inputs. Predict what a user might type, then decide which check would deal with it.

Computer Science · Validation

The 13-year-old who was turned away

A coach wants ages 6 to 13 inclusive to join the junior team. A 13-year-old types 13 and is told they cannot join. Which line is the problem?

The coach's program — which line goes wrong?

Computer Science · Validation

Now you build it

Build the junior-team age check line by line. Choose the right line for each gap.

  1. while True:The loop keeps asking until something says break.
  2. try:
  3. age = int(input("Enter your age: "))The code that converts the input to an integer goes in the try block.
  4. missing step
Which line is step 4?

Computer Science · Exam practice

Explain it in your own words

while True: try: age = int(input("Enter your age: ")) if age >= 6 and age <= 13: break else: print("You must be between age 6 and 13 to join") except ValueError: print("Please enter a whole number")

(a) Give two reasons for validating data. (b) A user of the junior-team program types ten, then 15, then 13. Describe what the program does with each entry. [5 marks]

0 words · your answer stays on this page and is not sent anywhere.

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

1Validation checks data is acceptable before the program uses it.
2A range check confirms a number falls within a set range.
3A range check is built from iteration: while True repeats the question, an if tests the range and break ends the loop.
4try and except catches a non-number so the program shows a message instead of crashing.
5Include both end values by using >= for the lower limit and <= for the upper limit.

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?
Checking that data is acceptable by comparing it against a set of criteria before the program uses it.
Give the four purposes of validation.
It ensures data is reasonable, helps prevent crashes, helps maintain accuracy and reliability, and improves the user experience by catching mistakes early.
What is a crash?
When a program suddenly stops working because of an error it cannot recover from.
What does a range check do?
It confirms that numerical input falls within a set range.
What does inclusive mean for a range check?
Both end values are allowed, so the input is compared with the lower limit using >= and the upper limit using <=.
Why is a range check put inside a loop?
A program only asks again if it was programmed to. The loop keeps asking until valid data is entered, otherwise it runs with invalid data and may crash.
Which statement ends a range check loop?
break, which runs once the input is valid.
What goes in the try block of a range check?
The code that converts the input to an integer.
What happens when a ValueError occurs in the try block?
Python jumps to the except block, which displays a message instead of crashing, and the question is asked again.
Name the three validation checks.
Empty string check, minimum length check and range check.
What does an empty string check ensure?
That the user has entered something.
What is robust code?
Code that handles unexpected input or errors without crashing.

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 posted

More Edexcel GCSE Computer Science topics

See the full Edexcel Computer Science curriculum →

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.