Combining validation checks in a reusable function
A function that runs existence, type and range checks in order, then returns a valid age (or True/False) so you can reuse the same checks for every input.
Understanding Combining validation checks in a reusable function in VCE Applied Computing U1O2
What it does: The existence check comes first and stops empty input before the type or range checks are tried. Calling the function again applies the same checks to a new input without rewriting any validation code. Calling the function and storing its returned value in age gives you a number that has already passed every check. Both conditions must be true, so use 'and'. With 'or', almost any number passes, including -20 or 500. Reusing one function avoids repeated code and means a fix only has to be made in one place.
Common mistakes: The existence check compares text with an empty string, so it needs the comparison operator == instead of the assignment operator =. It should be: if text == "":. A function definition line must end with a colon. It should be: def get_valid_age():.
Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.
This module's key terms are also in a printable crossword (Modules 89–124) and word search (Modules 8–124).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
def get_valid_age():Fill in the Blanks
Spot the Error
Student with a class code? Join your class →