Combining validation checks in a validation function
A validation function that runs the existence, type and range checks in order and returns True or False, so the same checks can be reused for many inputs and tested.
Understanding Combining validation checks in a validation function in VCE Software Development U3O1
What it does: Each check only runs if the earlier ones passed. The empty check comes first, then the type check, and int() is only used once the text is known to be digits. A validation function returns True or False. The calling code then decides what to do next, such as accepting the value or asking again. Reusing one function reduces duplication. If the rule changes, such as a new maximum age, you fix it in one place and all three inputs are updated. The def line creates one reusable function that wraps all the validation checks and takes the entered text as its parameter.
Common mistakes: The existence check must catch an empty entry. Using != returns False for every non-empty value. It should be if text == "": so that only blank input is rejected. The type check should reject text that is NOT made of digits, so it needs not. As written, valid numbers return False. It should be if not text.isdigit():.
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 114–124) and word search (Modules 114–141).
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 is_valid_age(text):Fill in the Blanks
Spot the Error
Student with a class code? Join your class →