# Chapter 30 example: a BankAccount whose methods guard the balance. class Account: def __init__(self, owner): self.owner = owner self.balance = 0 def deposit(self, amount): if amount > 0: self.balance = self.balance + amount def withdraw(self, amount): if amount > 0 and amount <= self.balance: self.balance = self.balance - amount return True return False def get_balance(self): return self.balance acc = Account("Keiko") acc.deposit(100) acc.withdraw(30) print(acc.get_balance()) # 70 # TODO: add a .can_afford(amount) method that returns True when the # balance is at least amount, and use it before withdrawing.