← VCE Software Development U3O1

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

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 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.

Learning
Duo
A validation function is checking a typed age. Which check order is correct?
Range, then type, then existenceExistence, then type, then range
Matches
def is_valid_age(text): if 0 <= int(text) <= 120: return True if not text.isdigit(): return False
↓
The range check runs before the type check, so int(text) crashes on input like "abc". The type check must come before the range check.
Applying
Meanings
Term
def is_valid_age(text):
Meaning
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.
Practice
def is_valid_age(text):
Testing
Gaps
if text "":
Check Answer
Spots
1def is_valid_age(text):
2 if text != "":

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

The 'Galaxy Phoenix' badge — Orbit Champion, 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. A validation function is checking a typed age. Which check order is correct?
A. Existence, then type, then range ✓
B. Range, then type, then existence
Check that something was entered first, then that it is the right type (a whole number), and only then check the range. You cannot compare the range of text that is not a number.
2. What does is_valid_age(text) return when every check passes?
A. True ✓
B. The age as text
A validation function returns True or False. The calling code then decides what to do next, such as accepting the value or asking again.
3. Which line is the existence check in is_valid_age(text)?
A. if int(text) > 120: return False
B. if text == "": return False ✓
An existence check makes sure something was entered. Comparing with an empty string does this. The other line is a range check and would crash on empty text.
4. Why should the type check come before the range check?
A. Range checks run faster when they come second
B. Comparing text such as "abc" with a number would cause a runtime error ✓
If the value is not a number, a range comparison like int(text) <= 120 fails. The type check stops invalid input before the range check runs.
5. Which approach lets you reuse validation for many inputs?
A. Put the checks in one function and call it for each input ✓
B. Copy and paste the checks next to every input() call
A single validation function is written once, tested once and reused. Copying code makes the program longer and harder to maintain.

Match the Pairs

def is_valid_age(text): → Defines a validation function that takes the user's text and will return True or False
if text == "": return False → Existence check that rejects empty input first by returning False
Checks run in order: existence, then type, then range, so the range check never sees non-numbers → if text == "": return False if not text.isdigit(): return False return 0 <= int(text) <= 120
One validation function can be reused for many different inputs → is_valid_age("17") is_valid_age("abc") is_valid_age("")
def is_valid_age(text): if 0 <= int(text) <= 120: return True if not text.isdigit(): return False → The range check runs before the type check, so int(text) crashes on input like "abc". The type check must come before the range check.
def is_valid_age(text): if text == "": print("Missing") return 0 <= int(text) <= 120 → The existence check only prints a message and does not return False, so the function carries on and int("") crashes.

Key Term

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

Fill in the Blanks

1. if text == "": return False
Hint: Type the comparison operator that tests whether the input is equal to an empty string (the existence check).
2. if not text.isdigit(): return False
Hint: Type the string method that does the type check by testing whether every character is a digit.
3. age = int(text) return age >= 0 and age <= 120
Hint: Type the Boolean operator that requires both sides of the range check to be true.
4. if text == "": return False, "Age is required"
Hint: Type the Boolean value that shows the check failed, returned alongside the error message.
5. for text in ages: if is_valid_age(text): print("Accepted")
Hint: Type the name of the validation function being reused to check each item in the list.

Spot the Error

Question 1
A def is_valid_age(text):
B if text != "":
C return False
D if not text.isdigit():
E return False
F return int(text) <= 120
Line B — 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.
Question 2
A def is_valid_age(text):
B if text == "":
C return False
D if text.isdigit():
E return False
F return int(text) <= 120
Line D — 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():.
Question 3
A def is_valid_age(text):
B if text.strip() = "":
C return False
D if not text.isdigit():
E return False
F return int(text) <= 120
Line B — A single = assigns a value and is a syntax error inside an if. A comparison needs ==, so it should be if text.strip() == "":.
Question 4
A def check_age(text):
B if len(text) == 1:
C return "Enter an age"
D if not text.isdigit():
E return "Not a number"
F return ""
Line B — The existence check should only trigger when nothing was entered. Testing for length 1 flags one-digit ages and misses the empty string. It should be if text == "": (or len(text) == 0).
Question 5
A def is_valid_mark(text):
B if not text.isdigit():
C return True
D mark = int(text)
E return 0 <= mark <= 100
Line C — When the type check fails the input is not a number, so the function must return False. Returning True would let invalid data through. It should be return False.

Student with a class code? Join your class →