Skip to content

28. Bank Account with Validation

Difficulty: Easy · Topic: OOP & classes

Write a class BankAccount(owner, balance=0) and an exception InsufficientFunds (a subclass of Exception):

Your code runs in CPython on our servers against 6 tests, including hidden ones.

Sample test

acc = BankAccount('Asha', 100)
acc.deposit(50)
acc.withdraw(30)
(acc.balance, acc.history)

Expected: (120, [('deposit', 50), ('withdraw', 30)])

Hints

Hint 1Keep the real value in self._balance and expose it with @property; a property with no setter cannot be assigned.
Hint 2Validate first, then change state, so a failed call leaves nothing half-done.
Hint 3class InsufficientFunds(Exception): pass is a complete exception class.

Learn the concepts

More Python problems

All Python problems