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
| Word | What it means | Example |
|---|---|---|
| Validation | A check that data makes sense before the program uses it. | |
| Valid | Data that passes the check. | an age of 16 |
| Invalid | Data that fails the check. | an age of 400 |
| Presence check | Checks that something was typed. | |
| Length check | Checks how many characters there are. | |
| Range check | Checks that a number is between a lowest and a highest value. | |
| Type check | Checks that the data is the right kind, such as a whole number. | |
| Format check | Checks that the data follows a pattern. | an email has an @ |
| Check digit | An 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
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
| Check | The question it asks | Example |
|---|---|---|
| Presence | Is there something? | A name is not empty |
| Length | Is it long or short enough? | A password has 8 to 20 characters |
| Range | Is the number between the lowest and highest? | An age is from 0 to 120 |
| Type | Is it the right kind of data? | An age is a whole number |
| Format | Does it follow the pattern? | An email has an @ and a dot |
| Check digit | Does 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
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
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?
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 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.