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

age="34"

Step 1. The user types 34. input() always returns a string, so age holds the text "34", not the number 34.

Step 1 of 3Called right: 0 of 1

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.

8 of 8 still to sort.

Each condition does exactly one job. Decide which one before you read the reason.

Exam line: Name a check by what it tests: empty or not, how many characters, which numbers, which type, which pattern, which allowed values.
Watch out: Lookalikes: 8 <= len(password) <= 20 counts characters, so it is a length check. 1 <= age <= 120 compares a number, so it is a range check.

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.

Python 3
  1. value = input("Enter a score from 0 to 100: ")
  2. if int(value) <= 100 and value.isdigit():
  3. print("Score accepted")
  4. else:
  5. 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.

Computer Science · Validation or verification?

It passed, so it is right... isn't it?

A form checks that an age is between 1 and 120. Aisha means to type 43 but types 34, and the form accepts it.

Which idea is closest to what you think right now?
How sure are you?

Exam line: Validation asks whether data is reasonable and obeys the rules. Verification asks whether data is exactly what was intended.

Computer Science · Build it

A password routine, with a second look

Build a routine that asks for a password twice. It is valid when it is 8 to 20 characters long, and it is only accepted when it is valid AND both entries match. Choose each missing line.

  1. def valid_password(password):A function takes the entry as a parameter, so the same check can be reused wherever a password is needed.
  2. missing step
Which line is step 2?

Exam line: A function returns True or False for the rules; a while True loop with break asks twice, compares with ==, and only leaves when both checks pass.

Computer Science · Change it safely

Spot the loop that never ends

Rav added a counter so the routine gives up after 3 failed attempts. But a user who keeps typing rubbish is still asked for ever. Which line is the fault?

Rav's changed routine — which line goes wrong?

Exam line: Whenever you add a rule, a limit or a counter, ask: what changes the tested value on every pass, so the loop can still end?

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

1A validation routine tests an entry against rules and only accepts it when every rule is met, asking again with a helpful message until it is.
2Validation shows data is sensible and obeys the rules; only verification (double entry or a visual check) asks whether it is what was intended.
3The six checks are presence, length, range, type, format and lookup. In Python 3, input() returns a string, so convert before comparing numbers.
4< and > exclude the limit; <= and >= include it. Put value.isdigit() before int(value) so a later check cannot fail on bad data.
5The variable tested in the while condition must be updated inside the loop, or the loop never ends.

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?
It tests an entry against one or more rules and only accepts the value when every rule is met.
Why can a valid value still be wrong?
Validation checks only that data is sensible and follows the rules. An age typed as 34 instead of 43 still passes a range check of 1 to 120.
What does verification check, and how?
That data matches what was intended, for example by double entry or a visual check (show it back and ask Y/N).
What type does input() return in Python 3?
Always a string. Convert with int() or float() before comparing with numeric limits.
Which Python tests give a presence check and a length check?
Presence: value.strip() == "" (empty). Length: len() compared with a minimum and/or maximum, e.g. 8 <= len(password) <= 20.
Which operators include the limit in a range check?
<= and >= include it. < and > exclude it. 1 <= age <= 120 accepts 1 and 120 themselves.
What does value.isdigit() test?
It is True only when the string is non-empty and every character is a digit. It is a type check made before conversion.
What stops int("abc") crashing the program?
Put the conversion in try: and catch the failure with except ValueError:, then reject the entry.
What is a format check in Python 3?
It tests the pattern of a string: a character by position (value[0].isalpha()), its start or end (startswith, endswith), or a required character ("@" in value).
How do you make a lookup check ignore case?
Convert the entry first with upper() or lower(): choice.upper() in ["A", "B", "C"].
Why does the order of checks matter with and?
Python reads left to right and stops as soon as the result is known. value.isdigit() and int(value) <= 100 only converts when the entry is all digits.
How does a repeat-until-valid loop work?
Ask once before the while loop. The condition is true while the entry is invalid. Inside, show what was wrong and ask again so the tested variable changes.
How do you write a double-entry verification routine?
Ask for the data twice, compare with ==, show a message on a mismatch and accept only when they match, in a loop, for example while True: with break.
How can a routine limit the number of attempts?
Use a counter increased on each failed attempt and test it in the loop condition: while not valid and attempts < 3.

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 Eduqas GCSE Computer Science topics

See the full Eduqas Computer Science curriculum →

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.