GCSE · Computer Science · Eduqas · Spec C500QS
Validation and verification routines in code
Type abc into a program that wants an age. Does it ask again politely, or fall over? A few lines of code decide, and you are about to write them.
Computer Science · Validation
Be the program
One routine, three different users. Call each result before the screen shows it.
Step through the routine. When a step is marked, make your call before it appears.
age = input("Enter your age (1 to 120): ")while not (age.isdigit() and 1 <= int(age) <= 120):print("Age must be a whole number from 1 to 120")age = input("Enter your age (1 to 120): ")print("Age accepted")
Variables
Step 1. The user types 34. input() always returns a string, so age holds the text "34", not the number 34.
The "while condition" row shows what the whole while line works out to. The program does not store it; it is there to help you read the line.
Computer Science · Types of check
What is this line checking?
Which kind of check does each line of Python perform?
Still to sort
Presence check (0)
Was anything entered at all?
Where the line is: A presence check asks whether the entry is empty. A length check asks how many characters there are.
Length check (0)
How many characters?
Where the line is: A length check uses len() on a string. A range check compares a number with limits.
Range check (0)
Is the number between the limits?
Where the line is: A range check compares a number with a lower and upper limit. Counting characters is a length check.
Type check (0)
Is it the right kind of data?
Where the line is: A type check tests the kind of data (here, all digits) before the value is converted.
Format check (0)
Does the text follow a pattern?
Where the line is: A format check tests a pattern: a character by position, the start or end, or a required character such as "@".
Lookup check (0)
Is it one of the allowed values?
Where the line is: A lookup check asks whether the entry is one of a list of allowed values. "@" in value tests a character inside a string, which is a format check.
Each condition does exactly one job. Decide which one before you read the reason.
Computer Science · Crash and fix
Break it, then fix it
The user types abc at the prompt. Press Run and see what happens. Then pick the replacement for the flagged line that makes the routine reject abc politely.
- value = input("Enter a score from 0 to 100: ")
- if int(value) <= 100 and value.isdigit():
- print("Score accepted")
- else:
- print("Whole numbers only, please")
The messages shown are what Python 3 reports for these exact programs. This page does not run code; every result is written in advance.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Make your program refuse bad input, then check the good input is really the input you meant.
What you need to know
- A validation routine tests an entry against one or more rules and only accepts it when every rule is met.
- Validation only checks data is sensible; verification checks it matches what was intended. A valid value can still be wrong.
Have a goSam says: "My sign-up form checks every password is 8 to 20 characters, so every password on it is exactly what the user meant." Is Sam right?
No. Passing the length rule only shows a password is sensible, not that it is the one the user meant.
Validation can only test rules. Showing data is what was intended is the job of verification.
- In Python 3, input() always returns a string, so convert with int() or float() before comparing with numeric limits.
- Six checks: presence, length, range, type, format and lookup. len() measures length; value.isdigit() tests the text is non-empty and all digits.
- A range check compares with limits: < and > exclude the limit itself, <= and >= include it.
Have a goA game says players must be older than 18, so exactly 18 is not allowed. Which operator goes in age ? 18: > or >=?
>
> excludes the limit itself, so 18 fails; >= would let 18 in.
- and, or and not combine rules. Python stops as soon as the result is known, so put the safe check first.
- A failed conversion such as int("abc") raises a ValueError; try/except ValueError lets the routine reject it instead of crashing.
Have a goThe user types abc and your code runs int(value). Which error is raised, and which two keywords stop the crash?
A ValueError. try and except ValueError stop it.
int() cannot turn "abc" into a number; the try/except lets the routine reject the entry instead of the program crashing.
- A repeat-until-valid loop asks once before the while, asks again inside it, and shows what was wrong each time.
- Verify by double entry (type it twice, compare with ==) or by a visual check (show it back and ask Y/N).
- Changing a routine can mean new limits, rules or messages. A counter such as attempts < 3 caps the number of tries.
The big picture
A validation routine keeps asking until the entry obeys every rule, and the small choices matter: the operator at the boundary, the order of the checks and updating the entry inside the loop. Validation only shows data is sensible; verification, by double entry or a visual check, asks whether it is what was intended.
Key points
Worked example
Problem
Write a routine that asks how many tickets (1 to 10), keeps asking until the entry is acceptable, tells the user what was wrong each time, and never crashes when the user types letters. Use try and except.
⚠ Watch out
Writing int(value) before checking the text is all digits. One entry of abc and the program crashes with a ValueError instead of asking again.
Memory hook
Validation is the bouncer: "Do you meet the rules?" Verification is the friend asking: "Is that really what you meant?" The bouncer can let in a wrong answer; the friend catches it.
Check yourself
Write the one-line condition that accepts a whole-number quantity from 1 to 10 without crashing on letters. Then say whether you used < or <= and why.
Flashcards
(14)What does a validation routine do?
Why can a valid value still be wrong?
What does verification check, and how?
What type does input() return in Python 3?
Which Python tests give a presence check and a length check?
Which operators include the limit in a range check?
What does value.isdigit() test?
What stops int("abc") crashing the program?
What is a format check in Python 3?
How do you make a lookup check ignore case?
Why does the order of checks matter with and?
How does a repeat-until-valid loop work?
How do you write a double-entry verification routine?
How can a routine limit the number of attempts?
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 Eduqas GCSE Computer Science topics
- Presenting test outcomes with commentary
- Program a solution to requirements
- Refining code during development
- Selection in programs
- Sequence in programs
- String manipulation in code
- Subroutines with well-defined interfaces
- Typical, extreme and erroneous test data
- Understanding revised requirements
- Using a variety of data types
- Using testing outcomes to inform development
- Variables, operators, inputs, outputs and assignment
How this lesson was checked. This Eduqas GCSE Computer Science (specification C500QS)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 15 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.