OOP – Practical Application, online

Six activities your students do on their own devices. See who's finished, and their scores.

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.

Discover other journeys with this module

What students will learn in this module

Every question in Module 53

Quick questions

  1. Why use OOP for a bank account?AGroups related data and behavior togetherBIt's faster
  2. What should __init__ set for a BankAccount?Aowner and balanceBOnly the class name
  3. Why check balance before withdrawing?APython requires itBTo prevent overdraft
  4. acc.deposit(50) what should this do?ACreate a new accountBAdd 50 to self.balance
  5. Can one class have many objects?AYes each with their own dataBNo only one

Key terms

Students type the meaning of:

  • Encapsulation
  • State
  • Behavior
  • Real-world modeling

Match the pairs

def __init__(name):Match it to what it does
acc.balance()Match it to what it does
def __init__(self, name):Match it to what it does
self.balance += amountMatch it to what it does
round(3.14159, 2)Match it to what it does
abs(-7)Match it to what it does

Fill in the blanks

  1. class BankAccount:
        def deposit(self, amount):
            self._______ += amount
  2. class TodoList:
        def add(self, task):
            self.tasks.______(task)
  3. class BankAccount:
        def withdraw(self, amount):
            if amount <= self._______:
                self.balance -= amount
  4. acc = BankAccount('Alice', 100)
    acc._______(50)
  5. 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