OOP – Practical Application, online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
OOP – Practical Application in one minute
What students need to know
__init__ runs automatically when a new object is created. Methods can read and modify the object's own attributes via self.
OOP keeps the data (balance) and the operations (deposit, withdraw) in one place. __init__ stores the initial state — every account needs an owner and a starting balance.
Mistakes students make
- No balance check could go negative. Add validation.
- Must use self.balance balance alone is undefined here.
- tasks = [] is a local variable. Use self.tasks = [] to store on object.
- Missing self as first parameter.
PREVIOUS · MODULE 52OOP – String Representation (__str__)
YOU ARE HERE · MODULE 53OOP – Practical Application
NEXT · MODULE 54OOP – Class Variables vs Instance Variables
What students will learn in this module
Every question in Module 53
Quick questions
- Why use OOP for a bank account?AGroups related data and behavior togetherBIt's faster
- What should __init__ set for a BankAccount?Aowner and balanceBOnly the class name
- Why check balance before withdrawing?APython requires itBTo prevent overdraft
- acc.deposit(50) what should this do?ACreate a new accountBAdd 50 to self.balance
- Can one class have many objects?AYes each with their own dataBNo only one
Key terms
Students type the meaning of:
EncapsulationStateBehaviorReal-world modeling
Match the pairs
def __init__(name):Match it to what it doesacc.balance()Match it to what it doesdef __init__(self, name):Match it to what it doesself.balance += amountMatch it to what it doesround(3.14159, 2)Match it to what it doesabs(-7)Match it to what it doesFill in the blanks
class BankAccount: def deposit(self, amount): self._______ += amountclass TodoList: def add(self, task): self.tasks.______(task)class BankAccount: def withdraw(self, amount): if amount <= self._______: self.balance -= amountacc = BankAccount('Alice', 100) acc._______(50)class Score: def ________(self): self.points = _
Spot the error
Question 1
Aclass BankAccount:B def withdraw(self, amount):C self.balance -= amount
Question 2
Aclass BankAccount:B def deposit(self, amount):C balance += amount
Question 3
Aclass TodoList:B def __init__(self):C tasks = []D def add(self, task):E self.tasks.append(task)
Question 4
Aclass BankAccount:B def deposit(amount):C self.balance += amount
Question 5
Aclass BankAccount:B def __init__(self):C self.balance = 0D def deposit(self, amount):E self.balance += amount
