Deviopath › Python

Bank Account with Overdraft Protection

Medium · Classes

A class bundles data with the methods that change it. A @property lets callers read a value like an attribute (account.balance) without being able to assign it directly. Methods should raise an exception when asked to do something invalid, and leave the object unchanged.

Complete BankAccount (amounts are whole cents, the balance starts at 0):

- balance: a read-only property with the current balance. - deposit(amount): adds amount. - withdraw(amount): subtracts amount. If it is more than the balance, raise InsufficientFundsError("insufficient funds") (already defined). - Both methods raise ValueError("amount must be positive") if amount is 0 or less. Check this first.

The driver run_account is already written: after each operation it records the balance, or the error as text.

Next: Shapes with Inheritance and super()