1 · Learn and explore
Study this example, identify inputs and outputs, then trace the key variables. Run and adapt it in Python 3.
class BankAccount:
def __init__(self, owner, balance=0):
if balance < 0: raise ValueError("negative opening balance")
self.owner = owner
self._balance = balance
@property
def balance(self): return self._balance
def deposit(self, amount):
if amount <= 0: raise ValueError("deposit must be positive")
self._balance += amount
def withdraw(self, amount):
if amount <= 0 or amount > self._balance: raise ValueError("invalid withdrawal")
self._balance -= amount
account = BankAccount("Sam", 50)
account.deposit(25)
account.withdraw(10)
