← ACARA Digital Technologies Years 9–10

Building a modular program with functions, lists and classes

A program split into parts: a class for its things, a list of objects, and small functions called from main(), planned with diagrams and checked with tests.

Understanding Building a modular program with functions, lists and classes in ACARA Digital Technologies Years 9–10

What it does: The menu loop in main() repeats with a while loop, calling the right function for each choice. This line creates an object from the Pet class and adds it to the list of objects. Passing the list in as a parameter keeps the function reusable and easy to test on its own. main() is the control centre. The real work is done by small functions that each do one job. Create the object with Pet(...), then use append() to store it in the list. Small functions can be tested one at a time with assert or print(), which pinpoints the problem quickly.

Common mistakes: A class header must end with a colon. It should be class Pet:. Lists have no add() method. Use pets.append(pet) to put the new object on the end of the list.

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 681–707) and word search (Modules 629–707).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which plan shows how a big problem is broken into smaller tasks before you code?
A list of variable namesA hierarchy chart
Matches
def add_pet(name, age): pets.append(Pet(name, age)) return pets.append
↓
The function ends with a broken return that does not give back anything useful. Each small function should do one clear job, so this one should just add the pet and need no return.
Applying
Meanings
Term
modular program
Meaning
A program split into parts: a class for its things, a list of objects, and small functions called from main(), planned with diagrams and checked with tests.
Practice
class Book:
Testing
Gaps
Song:
Check Answer
Spots
1class Pet
2 def __init__(self, name):

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

The 'Nautilus' badge — Default Expert, 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 plan shows how a big problem is broken into smaller tasks before you code?
A. A hierarchy chart ✓
B. A list of variable names
A hierarchy chart breaks the main task into smaller sub-tasks. Each sub-task can then become a function.
2. In a modular program, where should the data about each thing (like a Student) live?
A. In one long main() function with many loose variables
B. In a class, with one object per thing ✓
A class describes the thing. Each object created from it holds that thing's own data.
3. Which function header handles a list of objects the best?
A. def show_all(students): ✓
B. def show_all(): # uses a global list
Passing the list in as a parameter keeps the function reusable and easy to test on its own.
4. What should main() mostly do in a modular program?
A. Run the menu loop and call small functions ✓
B. Hold all the code for every task
main() is the control centre. The real work is done by small functions that each do one job.
5. Which line adds a new object to a list called pets?
A. pets.Pet('Rex', 3)
B. pets.append(Pet('Rex', 3)) ✓
Create the object with Pet(...), then use append() to store it in the list.

Match the Pairs

pets = [] → Creates an empty list that will hold the program's objects, such as Pet objects
def show_all(pets): → A small function that receives the list of objects so it can handle them, for example printing each one
main() runs a menu loop that keeps asking the user what to do until they choose to quit → while choice != "q":
A class describes one kind of thing, and each object made from it is stored in the list → pets.append(Pet("Rex", 3))
def add_pet(name, age): pets.append(Pet(name, age)) return pets.append → The function ends with a broken return that does not give back anything useful. Each small function should do one clear job, so this one should just add the pet and need no return.
main print("Program finished") → main is written without brackets, so the function is never called and the program does nothing. It should be main().

Key Term

modular program
A program split into parts: a class for its things, a list of objects, and small functions called from main(), planned with diagrams and checked with tests.

Fill in the Blanks

1. class Song: def __init__(self, title): self.title = title
Hint: Type the keyword that begins defining a new blueprint for objects.
2. songs = [] songs.append(Song("Hello"))
Hint: Type the list method that adds one item to the end of the list.
3. def show(songs): for song in songs: print(song)
Hint: Type the name of the list parameter that the loop goes through.
4. def main(): while True: choice = input("Choice: ")
Hint: Type the keyword for a loop that keeps the menu repeating as long as its condition holds.
5. songs = [] songs.append(Song("Hello")) assert len(songs) == 1
Hint: Type the built-in function that counts how many items are in the list.

Spot the Error

Question 1
A class Pet
B def __init__(self, name):
C self.name = name
D pets = []
Line A — A class header must end with a colon. It should be class Pet:
Question 2
A def add_pet(pets, name):
B pet = Pet(name)
C pets.add(pet)
D return pets
Line C — Lists have no add() method. Use pets.append(pet) to put the new object on the end of the list.
Question 3
A def show_all(pets):
B for pet of pets:
C print(pet.name)
Line B — Python for loops use the keyword in, not of. It should be for pet in pets:
Question 4
A def main():
B pets = []
C if choice = 'y':
D if choice == 'y':
E add_pet(pets, 'Rex')
Line C — A single = assigns a value. To compare in an if statement use ==. It should be if choice == 'y':
Question 5
A def count_big(pets, size):
B total = 0
C for pet in pets:
D if pet > size:
E total += 1
F return total
Line D — pet is a whole object, so it can't be compared with a number. Compare the attribute: pet.size > size.

Student with a class code? Join your class →