Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Validation: checking input

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

The age that was "banana"

A student club has a sign-up form. Somebody types "banana" for their age. Somebody leaves their name empty. Somebody types an age of 400.

The program saves it all. Now the club list is full of wrong data. Good programs stop bad data before it gets in.

Key words

WordWhat it meansExample
ValidationA check that data makes sense before the program uses it.
ValidData that passes the check.an age of 16
InvalidData that fails the check.an age of 400
Presence checkChecks that something was typed.
Length checkChecks how many characters there are.
Range checkChecks that a number is between a lowest and a highest value.
Type checkChecks that the data is the right kind, such as a whole number.
Format checkChecks that the data follows a pattern.an email has an @
Check digitAn extra digit worked out from the other digits. It finds typing mistakes.the last digit of a barcode

The cells students fill in are left blank in this preview.

What validation does

Validation asks: does this data make sense? It runs when a user types something, before the program uses the data. If the data is invalid, the program asks again.

Validation cannot tell if data is true. An age of 25 passes a range check even if the user is 15.

Try it: presence and length

Code: python
name = ""
password = "abc123"

if len(name) == 0:
    print("Please type your name.")
if len(password) < 8:
    print("The password needs at least 8 characters.")

# Give name and password good values. The messages disappear.

The five checks

CheckThe question it asksExample
PresenceIs there something?A name is not empty
LengthIs it long or short enough?A password has 8 to 20 characters
RangeIs the number between the lowest and highest?An age is from 0 to 120
TypeIs it the right kind of data?An age is a whole number
FormatDoes it follow the pattern?An email has an @ and a dot
Check digitDoes the last number fit the others?The next lesson

The cells students fill in are left blank in this preview.

Try it: type and range

Code: python
text = "250"

if not text.isdigit():
    print("Type check failed: use digits only.")
elif int(text) > 120:
    print("Range check failed: the age is more than 120.")
else:
    print("Age accepted.")

# Try "abc", "16", "-5" and "250".

Try it: a format check

Code: python
email = "[email protected]"

if "@" in email and "." in email:
    print("The format looks right.")
else:
    print("An email needs an @ and a dot.")

# Try "sam.mail.com".

Try it: is it a real date?

Code: python
from datetime import datetime

text = "25/12/2026"

try:
    when = datetime.strptime(text, "%d/%m/%Y")
    print("A real date:", when.strftime("%A %d %B %Y"))
except ValueError:
    print("Please write the date like 25/12/2026.")

# Try "31/02/2026" and "tomorrow". Both fail the check.

isdigit has limits

data-callouts.warning.label

"16".isdigit() is True. But "-5".isdigit() and "12.5".isdigit() are False, because of the minus sign and the dot. isdigit() is True only when every character is a digit. For whole numbers from 0 up, that is just what you want.

Quick check: which check?

Have a go

Quick check: which check?

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

The lesson goes on

7 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.