← VCE Applied Computing U1O2

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():.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

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.

Learning
Duo
Why put existence, type and range checks inside one function like get_valid_age()?
So the program only has to check the first age enteredSo the same checks can be reused every time an age is needed
Matches
age = int(text) if text == "": return False
↓
The type check (int) runs before the existence check, so empty input crashes with an error. Check that the input exists first.
Applying
Meanings
Term
def get_valid_age():
Meaning
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.
Practice
def get_valid_age(text):
Testing
Gaps
if age "":
Check Answer
Spots
2 text = input("Age: ")
3 if text = "":

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Bear' badge — Forest Guardian, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Why put existence, type and range checks inside one function like get_valid_age()?
A. So the same checks can be reused every time an age is needed ✓
B. So the program only has to check the first age entered
A reusable function keeps the checks in one place, so you call it for each input instead of rewriting the code.
2. Which order should get_valid_age() apply its checks?
A. Existence, then type, then range ✓
B. Range, then type, then existence
Check something was entered first, then that it is a number, then that the number is in the allowed range. A range check on text or an empty value would crash.
3. Which line is the best way to use a function that returns a checked age?
A. age = get_valid_age() ✓
B. get_valid_age = age()
Calling the function and storing its returned value in age gives you a number that has already passed every check.
4. A function checks an age and returns True or False. What does returning False tell the program?
A. The data passed every validation check
B. The data failed at least one validation check ✓
True means the value is valid. False means it failed the existence, type or range check.
5. Which range check belongs inside get_valid_age() for ages 5 to 100?
A. if age >= 5 or age <= 100:
B. if age >= 5 and age <= 100: ✓
Both conditions must be true, so use 'and'. With 'or', almost any number passes, including -20 or 500.

Match the Pairs

def get_valid_age(): → Defines a reusable function that bundles all the age checks together so they can be called for every input
return age → Sends the checked age back to the code that called the function once it has passed every check
Run the checks in order: existence first, then type, then range → if text == "": return False
Reuse the same function for each input → age2 = get_valid_age()
age = int(text) if text == "": return False → The type check (int) runs before the existence check, so empty input crashes with an error. Check that the input exists first.
def get_valid_age(): age = int(input("Age: ")) if 0 <= age <= 120: print(age) → The function prints the age instead of returning it, so the caller gets no checked value back. Use return age.

Key Term

def get_valid_age():
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.

Fill in the Blanks

1. age = input("Age: ") if age == "": return False
Hint: Type the comparison operator that checks whether the input is the same as an empty string (existence check).
2. age = input("Age: ") if not age.isdigit(): return False
Hint: Type the string method that checks the text contains only digits (type check).
3. age = int(age) if age < 0 or age > 120: return False
Hint: Type the operator that tests whether the age is above the maximum allowed (range check).
4. if 0 <= age <= 120: return age return False
Hint: Type the keyword that sends the checked number back to the code that called the function.
5. first = get_valid_age() second = get_valid_age() print(first + second)
Hint: Type the name of the reusable function that gets a checked age for each input.

Spot the Error

Question 1
A def get_valid_age():
B text = input("Age: ")
C if text = "":
D return False
E return True
Line C — 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 == "":
Question 2
A def get_valid_age()
B age = input("Age: ")
C if not age.isdigit():
D return False
E return True
Line A — A function definition line must end with a colon. It should be: def get_valid_age():
Question 3
A def get_valid_age():
B age = int(input("Age: "))
C if age < 0 and age > 120:
D return False
E return age
Line C — No number is both below 0 and above 120, so 'and' means the range check never catches an invalid age. It needs 'or': if age < 0 or age > 120:
Question 4
A def get_valid_age():
B text = input("Age: ")
C if not text.isdigit():
D return True
E age = int(text)
F return 0 <= age <= 120
Line D — The type check should reject text that is not made of digits, so it must return False. Returning True would tell the program that bad input is valid. It should be: return False
Question 5
A def get_valid_age():
B text = int(input("Age: "))
C if text == "":
D return None
E return int(text)
Line B — Converting to int straight away crashes on empty or non-numeric input before the existence and type checks can run. Keep the input as text first: text = input("Age: ")

Student with a class code? Join your class →