← VCE Software Development U3O1

Setter methods that validate data

A setter method that checks a new value (such as a valid age range) before storing it in a private attribute, rejecting or reporting bad values so the object never holds invalid data.

Understanding Setter methods that validate data in VCE Software Development U3O1

What it does: The assignment to self._age sits inside the if block, so it only runs when the new age passes the validation check. The else branch reports the problem and does not assign to self._age, so the attribute keeps its previous valid value. A validating setter checks the value first, so the object never holds invalid data. The check must happen before the assignment. In the wrong version the bad value has already been stored. A setter that validates refuses bad values and keeps the old, valid data. It may also print a message or raise an error. 'or' lets every number through, because any number is either at least 0 or at most 120. 'and' needs both conditions to be true.

Common mistakes: A method must have self as its first parameter so it can reach the object's private attribute. It should be def set_age(self, age):. The function header is missing the colon at the end, which causes a syntax error. It should be def set_score(self, score):.

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 125–141) 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
What is the main job of a validating setter such as set_age(self, age)?
Store any value first and check it laterCheck the new value before storing it in the private attribute
Matches
def set_age(self, age): self._age = age if age >= 0: print("Age set")
↓
The value is stored before it is checked, so a negative age is saved anyway. The assignment must go inside the if block.
Applying
Meanings
Term
def set_age(self, age):
Meaning
A setter method that checks a new value (such as a valid age range) before storing it in a private attribute, rejecting or reporting bad values so the object never holds invalid data.
Practice
class Student:
Testing
Gaps
def (self, age):
Check Answer
Spots
1def set_age(age):
2 if 0 <= age <= 120:

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

The 'Narwhal' badge — Unicorn Coder, 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. What is the main job of a validating setter such as set_age(self, age)?
A. Check the new value before storing it in the private attribute ✓
B. Store any value first and check it later
A validating setter checks the value first, so the object never holds invalid data.
2. Which setter body keeps an age valid?
A. if 0 <= age <= 120: self._age = age ✓
B. self._age = age; if age < 0: print('Bad')
The check must happen before the assignment. In the wrong version the bad value has already been stored.
3. A student calls set_age(-5) on a Person object. What should a well-written setter do?
A. Reject the value and leave _age unchanged, reporting the problem ✓
B. Store -5 in _age anyway
A setter that validates refuses bad values and keeps the old, valid data. It may also print a message or raise an error.
4. Which range check correctly allows ages from 0 to 120 only?
A. if age >= 0 or age <= 120:
B. if age >= 0 and age <= 120: ✓
'or' lets every number through, because any number is either at least 0 or at most 120. 'and' needs both conditions to be true.
5. Why use a setter with validation instead of writing person._age = 500 directly?
A. Direct assignment always checks the range for you
B. The setter can stop invalid values like 500 from being stored ✓
Python does not check values on direct assignment. A setter is where you put the validation.

Match the Pairs

def set_age(self, age): → Defines a setter method that receives a new age so it can be checked before being stored
if 0 <= age <= 120: → A range check inside the setter that only allows ages from 0 to 120
Only store the new value in the private attribute after the check passes → if age >= 0: self._age = age
Report a bad value instead of storing it, so the object keeps its old valid data → else: print("Invalid age")
def set_age(self, age): self._age = age if age >= 0: print("Age set") → The value is stored before it is checked, so a negative age is saved anyway. The assignment must go inside the if block.
def set_age(self, age): if 0 <= age <= 120: age = self._age → The assignment is backwards. It overwrites the parameter with the old attribute instead of storing the new age in self._age.

Key Term

def set_age(self, age):
A setter method that checks a new value (such as a valid age range) before storing it in a private attribute, rejecting or reporting bad values so the object never holds invalid data.

Fill in the Blanks

1. def set_age(self, age): if 0 <= age <= 120: self._age = age
Hint: Type the name of the setter method that updates the age attribute
2. if age >= 0 and age <= 120: self._age = age else: print("Bad age")
Hint: Type the logical operator that requires both range conditions to be true
3. if 0 <= age <= 120: self._age = age else: print("Bad age")
Hint: Type the private attribute name that is only changed after the check passes
4. if age < 0 or age > 120: raise ValueError("Bad age") self._age = age
Hint: Type the keyword that rejects a bad value by signalling an error
5. if age < 0 or age > 120: return False self._age = age
Hint: Type the keyword that sends a result back to the caller to report the value was rejected

Spot the Error

Question 1
A def set_age(age):
B if 0 <= age <= 120:
C self._age = age
D else:
E print('Invalid age')
Line A — A method must have self as its first parameter so it can reach the object's private attribute. It should be def set_age(self, age):
Question 2
A def set_score(self, score)
B if 0 <= score <= 100:
C self._score = score
D else:
E print('Bad score')
Line A — The function header is missing the colon at the end, which causes a syntax error. It should be def set_score(self, score):
Question 3
A def set_age(self, age):
B if age < 0 or age > 120:
C print('Invalid age')
D pass
E self._age = age
Line D — Without return, the code carries on after reporting the error and stores the invalid age anyway. The line should be return so the bad value is rejected.
Question 4
A def set_age(self, age):
B if 5 <= age or age <= 18:
C self._age = age
D else:
E print('Age 5-18 only')
Line B — Using or makes the check true for almost any number, so invalid ages would be accepted. Both range limits must hold, so it should use and.
Question 5
A def set_age(self, age):
B if 0 <= age <= 120:
C age = self._age
D else:
E print('Invalid age')
Line C — The assignment is the wrong way round. It overwrites the parameter instead of updating the private attribute. It should be self._age = age

Student with a class code? Join your class →