Bank Account Class
What you're building
A BankAccount class that tracks a balance, records a transaction history, and refuses withdrawals that would overdraw the account.
Requirements
deposit(amount)β addamountto balance; return new balancewithdraw(amount)β subtractamountif funds are available; returnTrueon success,Falseon failurebalanceproperty β current balancehistoryproperty β list of(type, amount)tuples, e.g.("deposit", 100)
acc = BankAccount(100)
acc.deposit(50) # balance β 150
acc.withdraw(200) # False (overdraft)
acc.withdraw(40) # True, balance β 110
Stretch goals
- Add a
transfer(amount, other_account)method. - Raise
ValueErrorfor negative deposit/withdrawal amounts.
What this proves
OOP fundamentals β __init__, instance state, methods with return values, and defensive guards β the bread and butter of system design questions.
β‘ 40 XP
solution.pyRuns in your browser (Pyodide)
Loading editorβ¦
Tests4 to pass
deposit increases balance
withdraw succeeds when funds available
withdraw fails when insufficient funds
history records transactions
Run your code to check these.