Year 10Technologies 70 minAC9TDI10P09: implement, modify and debug modular programs, applying selected algorithms and data structures, including in an object-oriented programming language
We are learning to
- We are learning to read, debug and modify a modular object-oriented program.
- We are learning how classes, objects, attributes and methods organise a program.
- We are learning to choose data structures and algorithms that suit a task.
Success criteria: I can…
- I can explain the difference between a class and an object, using Book as an example.
- I can identify the data structures and algorithms used in the program.
- I can use a traceback and expected output to find and fix runtime and logic errors.
- I can add an attribute and a method to the correct class and test them.
Key competenciesDigital LiteracyCritical and Creative ThinkingNumeracy
Lesson sequence
70 min total- 1
Objects in the real world
Starter 0–8 minTeacher- Show the class a library book and a student ID card (or draw them).
- Ask: What does the library need to know about this book? (an ID, title, author, whether it is on loan)
- Ask: What can happen to a book? (it can be borrowed and returned)
- Say: “In object-oriented programming, a class is a blueprint. It bundles the data (attributes) and the actions (methods) for one kind of thing. Each real book is an object made from the Book class.”
- Write on the board: Book: attributes book_id, title, author, on_loan. Member: attributes member_id, name, loans; method can_borrow. Library: attributes books (a dictionary), members (a dictionary); methods lend, return_book, available, search.
Students- Name attributes and actions for a book and a member.
- Copy the class outline.
A library book and an ID card, or drawings of them - 2
Read the modules
Main 8–20 minTeacher- Students open the two files (Resources): library.py (the classes) and main.py (the program that uses them).
- Say: “This is a modular program. library.py holds the classes. main.py imports them with from library import Book, Member, Library. We can reuse library.py in other programs without copying it.”
- Walk through the Book class: __init__ runs when an object is created and self means this particular object.
- Ask:
- 1."Which data structure stores the books?" (a dictionary, with the book ID as the key)
- 2."Why a dictionary, not a list?" (we can find a book by its ID directly, without searching the whole list)
- 3."Which data structure stores one member's loans?" (a list of Book objects)
- 4."What algorithm does search use?" (a linear search: it checks every book's title)
- 5."What does available return?" (a list of books not on loan, sorted by title)
Students- Read both files and annotate each class with its attributes and methods.
- Answer the questions.
library.py (buggy version) and main.py (text in Resources) - 3
Debug: four bugs
Main 20–45 minTeacher- Say: “This version has four bugs. Run main.py, read the error or output, find the cause, fix it and run again. Record each bug in the debugging log.”
- Model the first: run main.py. It crashes with AttributeError: type object 'Member' has no attribute 'MAX_LOAN'. Read the traceback from the bottom up: the last line names the error, the line above it shows library.py, line 22, in can_borrow. Fix: MAX_LOAN becomes MAX_LOANS.
- Run again and compare with the expected output (Resources). Pairs find the remaining three bugs.
- Hints if pairs are stuck (give one at a time):
- Hint 1: "Ari does not have Coding for Beginners. Look at what lend changes. What should it also change?"
- Hint 2: "The Available list shows books that are on loan. Read the condition in available."
- Hint 3: "Search 'reef' finds nothing, but Coral Reef Survival exists. What is different about Reef and reef?"
- Tip: add a temporary print(member.loans) inside return_book to see what the list holds.
Students- Run, read the output, fix one bug at a time and test after each fix.
- Complete the debugging log.
Expected output and debugging log (text in Resources) - 4
Modify: test the limit and add a feature
Main 45–60 minTeacher- Ask pairs to add these lines to the end of main.py and predict the output before running:
- lib.add_book(Book("B5", "Desert Night Skies", "T. Evans"))
- print(lib.lend("B1", "M1"))
- print(lib.lend("B2", "M1"))
- print(lib.lend("B4", "M1"))
- print(lib.lend("B5", "M1"))
- Expected: three Ari borrowed lines, then Sorry, Ari already has 3 books.
- Ask: Why is the last loan refused even though Desert Night Skies is available? (Ari now has B1, B2 and B4, and can_borrow checks the length of his loans list against MAX_LOANS, which is 3)
- Ask: Why did we add a new book instead of trying B3? (Grace still has B3, so lend would refuse it for a different reason and we would not be testing the limit)
- Then set the modification task (Resources): add a times_borrowed attribute to Book, add 1 to it in lend, and write a most_popular method in Library.
- Say: “Change only the class that owns the data. Book owns times_borrowed; Library looks across all books.”
Students- Predict, run and explain the loan limit test.
- Implement times_borrowed and most_popular, then test them.
Modification task and model solution (text in Resources) - 5
Why classes?
Plenary 60–70 minTeacher- Questions
- 1."Which bug was hardest to find, and why?" (usually a logic error, because there is no error message, only wrong output)
- 2."Where did you add times_borrowed, and why there?" (in Book, because it describes one book)
- 3."How does putting the classes in library.py help?" (the code is organised, easier to test and can be reused by other programs)
- 4."What would change if the library had 10,000 books?" (the dictionary still finds a book by ID quickly, but search still checks every title)
- Exit ticket: students write one sentence explaining the difference between a class and an object, using Book as the example.
Students- Answer the questions.
- Write the exit ticket.
Key vocabulary
class
a blueprint that defines the attributes and methods for one kind of object
object
one instance made from a class, such as one particular book
attribute
a piece of data stored in an object, such as title
method
a function that belongs to a class and acts on its objects, such as lend
module
a separate Python file whose code can be imported into another program
logic error
a bug that makes a program give wrong results without crashing
Differentiation
Support
- Give the debugging log with the line numbers of the four bugs filled in, so students find the cause and fix it.
- Pair students and let them fix bugs 1 and 4 first, which give the clearest symptoms.
Extension
- Add a Member method that returns a list of the titles a member has on loan, and use it to print each member's loans.
- Change search so it also matches the author, and write two test cases that prove it works.
- Explain why available() returns a sorted copy rather than sorting self.books, and what would happen to the dictionary if it tried to sort it in place.
Accessibility
- Provide the code as files (not screenshots) so students can use screen readers, zoom and their editor's font settings.
- Allow students to record the debugging log by voice or as comments in the code.
- Use the editor's line numbers and a high-contrast theme for students with low vision.
Assessment
- Debugging log: four bugs with symptom, location, type, fix and retest.
- Running program: output matches the expected output exactly.
- Modification: times_borrowed is in Book, most_popular is in Library, and the test gives Coding for Beginners.
- Exit ticket: a correct distinction between a class (the blueprint) and an object (one book made from it).
Resources
- library.py (buggy version, give this to students): class Book: def __init__(self, book_id, title, author): self.book_id = book_id self.title = title self.author = author self.on_loan = False def __str__(self): status = "on loan" if self.on_loan else "available" return f"{self.book_id}: {self.title} by {self.author} ({status})" class Member: MAX_LOANS = 3 def __init__(self, member_id, name): self.member_id = member_id self.name = name self.loans = [] def can_borrow(self): return len(self.loans) < Member.MAX_LOAN class Library: def __init__(self): self.books = {} self.members = {} def add_book(self, book): self.books[book.book_id] = book def add_member(self, member): self.members[member.member_id] = member def lend(self, book_id, member_id): book = self.books[book_id] member = self.members[member_id] if book.on_loan: return f"Sorry, {book.title} is already on loan." if not member.can_borrow(): return f"Sorry, {member.name} already has {Member.MAX_LOANS} books." book.on_loan = True return f"{member.name} borrowed {book.title}." def return_book(self, book_id, member_id): book = self.books[book_id] member = self.members[member_id] if book not in member.loans: return f"{member.name} does not have {book.title}." book.on_loan = False member.loans.remove(book) return f"{member.name} returned {book.title}." def available(self): free = [book for book in self.books.values() if book.on_loan] return sorted(free, key=lambda book: book.title) def search(self, word): word = word.lower() return [book for book in self.books.values() if word in book.title]
- main.py: from library import Book, Member, Library lib = Library() lib.add_book(Book("B1", "Wildlife of the Daintree", "R. Hall")) lib.add_book(Book("B2", "Coding for Beginners", "S. Patel")) lib.add_book(Book("B3", "Coral Reef Survival", "J. Moore")) lib.add_book(Book("B4", "Backyard Veggie Gardens", "L. Brooks")) lib.add_member(Member("M1", "Ari")) lib.add_member(Member("M2", "Grace")) print(lib.lend("B2", "M1")) print(lib.lend("B2", "M2")) print(lib.lend("B3", "M2")) print(lib.return_book("B2", "M2")) print(lib.return_book("B2", "M1")) print("Available:") for book in lib.available(): print(" ", book) print("Search 'reef':", [book.title for book in lib.search("reef")])
- Expected output when all four bugs are fixed: Ari borrowed Coding for Beginners. Sorry, Coding for Beginners is already on loan. Grace borrowed Coral Reef Survival. Grace does not have Coding for Beginners. Ari returned Coding for Beginners. Available: B4: Backyard Veggie Gardens by L. Brooks (available) B2: Coding for Beginners by S. Patel (available) B1: Wildlife of the Daintree by R. Hall (available) Search 'reef': ['Coral Reef Survival']
- Output after fixing only bug 1 (for the teacher, so you can see what pairs will see next): Ari borrowed Coding for Beginners. Sorry, Coding for Beginners is already on loan. Grace borrowed Coral Reef Survival. Grace does not have Coding for Beginners. Ari does not have Coding for Beginners. Available: B2: Coding for Beginners by S. Patel (on loan) B3: Coral Reef Survival by J. Moore (on loan) Search 'reef': []
- Debugging log (one row per bug): Symptom (error message or wrong output) | Line and method | Type of error (runtime or logic) | Fix | Retested? Model answers: 1. AttributeError: no attribute MAX_LOAN | line 22, can_borrow | runtime | change MAX_LOAN to MAX_LOANS | yes. 2. Ari does not have Coding for Beginners after borrowing it | lend | logic | add member.loans.append(book) after book.on_loan = True | yes. 3. Available lists books that are on loan | available | logic | change if book.on_loan to if not book.on_loan | yes. 4. Search 'reef' returns [] | search | logic | compare with book.title.lower(), not book.title | yes.
- Modification task: 1. In Book.__init__, add self.times_borrowed = 0. 2. In Library.lend, add book.times_borrowed += 1 just before member.loans.append(book). 3. Add this method to Library and test it: def most_popular(self): return max(self.books.values(), key=lambda book: book.times_borrowed). Test it in a new file, test_popular.py, saved next to library.py: from library import Book, Member, Library lib = Library() for b in [Book("B1", "Wildlife of the Daintree", "R. Hall"), Book("B2", "Coding for Beginners", "S. Patel"), Book("B3", "Coral Reef Survival", "J. Moore"), Book("B4", "Backyard Veggie Gardens", "L. Brooks")]: lib.add_book(b) lib.add_member(Member("M1", "Ari")) lib.add_member(Member("M2", "Grace")) print(lib.lend("B1", "M1")) print(lib.lend("B2", "M1")) print(lib.lend("B4", "M1")) print(lib.lend("B3", "M1")) print(lib.return_book("B2", "M1")) print(lib.lend("B2", "M2")) print(lib.most_popular().title) Output of the test: Ari borrowed Wildlife of the Daintree. Ari borrowed Coding for Beginners. Ari borrowed Backyard Veggie Gardens. Sorry, Ari already has 3 books. Ari returned Coding for Beginners. Grace borrowed Coding for Beginners. Coding for Beginners. (Coding for Beginners was borrowed twice; if several books are tied, max returns the first one it finds.)
- Computers with Python 3 and an editor that can open two files (IDLE, Thonny, VS Code or an online Python environment that supports multiple files)