← VCE Applied Computing U1O2

Setter methods that check new values

A setter method that changes a private attribute only after checking the new value is valid, such as rejecting a negative amount, so the object never holds bad data.

Understanding Setter methods that check new values in VCE Applied Computing U1O2

What it does: The else branch runs when validation fails, so the balance stays unchanged and the user is told the value was rejected. The getter returns the private value for reading, and it works alongside set_balance, which controls how the value is changed. A validating setter checks the new value with an if statement before it changes the private attribute. The second version accepts any value, even a bad one. A validating setter rejects -50, so the object keeps its old, valid balance. Putting the assignment inside the if block means it only runs for valid values. Outside the block, it runs every time and the check is useless.

Common mistakes: The setter must update the private attribute self._balance. Writing to self.balance creates a different public attribute, so the checked value is never stored in the right place. The getter must use self to reach the attribute. It should be return self._balance, because _balance on its own is not a defined variable.

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 8–88) 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
Which setter protects the private attribute _balance from a negative value?
def set_balance(self, amount): self._balance = amountdef set_balance(self, amount): if amount >= 0: self._balance = amount
Matches
def set_balance(self, amount): self._balance = amount
↓
The setter stores the value without checking it, so a negative amount could be saved and the object would hold bad data
Applying
Meanings
Term
def set_balance(self, amount):
Meaning
A setter method that changes a private attribute only after checking the new value is valid, such as rejecting a negative amount, so the object never holds bad data.
Practice
class Account:
Testing
Gaps
def (self, amount):
Check Answer
Spots
2 if amount >= 0:
3 self.balance = amount

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

The 'Hyena' badge — Savannah Debugger, 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. Which setter protects the private attribute _balance from a negative value?
A. def set_balance(self, amount): self._balance = amount
B. def set_balance(self, amount): if amount >= 0: self._balance = amount ✓
A validating setter checks the new value with an if statement before it changes the private attribute. The second version accepts any value, even a bad one.
2. What is the main job of a setter method that checks values?
A. Change a private attribute only if the new value is valid ✓
B. Change a private attribute to whatever value it is given
The point of a setter is control. It checks the value first so the object never holds bad data.
3. Which line inside set_balance correctly rejects a bad value?
A. if amount < 0: self._balance = amount
B. if amount < 0: print("Invalid amount") ✓
When the value is invalid, the setter should refuse it and leave _balance unchanged. Assigning it anyway defeats the check.
4. Account is an object with a private _balance. Which code uses the setter correctly from outside the class?
A. acc.set_balance(150) ✓
B. acc.set_balance = 150
A setter is a method, so you call it with brackets and pass the new value in. Using = would replace the method with a number instead of calling it.
5. Which pair is a getter and setter for _balance?
A. get_balance(self) returns self._balance; set_balance(self, amount) changes it ✓
B. get_balance(self) changes self._balance; set_balance(self, amount) returns it
The getter reads the private attribute and returns it. The setter changes it, after checking the new value.

Match the Pairs

def set_balance(self, amount): → Defines a setter method that receives a new value for the private balance attribute so it can be checked before it is stored
if amount >= 0: self._balance = amount → Checks the new value is valid and only then updates the private attribute
A setter can reject a bad value by refusing to change the attribute and giving feedback instead → else: print("Invalid amount")
A getter and setter pair lets code read a private attribute and change it safely → def get_balance(self): return self._balance
def set_balance(self, amount): self._balance = amount → The setter stores the value without checking it, so a negative amount could be saved and the object would hold bad data
def set_balance(self, amount): if amount >= 0: balance = amount → The value is saved to a local variable called balance, not to self._balance, so the object's attribute never changes

Key Term

def set_balance(self, amount):
A setter method that changes a private attribute only after checking the new value is valid, such as rejecting a negative amount, so the object never holds bad data.

Fill in the Blanks

1. def set_balance(self, amount): if amount >= 0: self._balance = amount
Hint: Type the name of the setter method that updates the balance.
2. if amount >= 0: self._balance = amount
Hint: Type the keyword that checks the new value is valid before the attribute changes.
3. if amount < 0: return
Hint: Type the comparison operator that spots a negative amount so the bad value can be rejected.
4. def get_balance(self): return self._balance
Hint: Type the name of the getter method that pairs with set_balance and reads the balance.
5. self._balance = amount
Hint: Type the private attribute name (with its underscore) that is only changed after the check passes.

Spot the Error

Question 1
A def set_balance(self, amount):
B if amount >= 0:
C self.balance = amount
D else:
E print('Invalid')
Line C — The setter must update the private attribute self._balance. Writing to self.balance creates a different public attribute, so the checked value is never stored in the right place.
Question 2
A def get_balance(self):
B return _balance
C def set_balance(self, amount):
D if amount > 0:
E self._balance = amount
Line B — The getter must use self to reach the attribute. It should be return self._balance, because _balance on its own is not a defined variable.
Question 3
A def set_balance(self, amount):
B if amount >= 0:
C amount = self._balance
Line C — The assignment is the wrong way round. The valid new value must be stored in the attribute: self._balance = amount.
Question 4
A def set_balance(amount):
B if amount < 0:
C print('Bad amount')
D else:
E self._balance = amount
Line A — Every method in a class needs self as its first parameter. Without it the setter cannot reach self._balance. It should be def set_balance(self, amount):.
Question 5
A def set_balance(self amount):
B if amount >= 0:
C self._balance = amount
D return True
E return False
Line A — Parameters must be separated by a comma. It should be def set_balance(self, amount):.

Student with a class code? Join your class →