Bank Account Ledger

medium
Unit 12 · Classes0/2 to milestone

Model a bank account properly: money uses decimal.Decimal (never floats), and the balance is derived from an append-only ledger of transactions, not stored separately.

Implement Account(owner, overdraft_limit="0") with:

  • deposit(amount, memo="") and withdraw(amount, memo=""). Amounts are strings or Decimals, must be positive, and have at most 2 decimal places (else ValueError). A withdrawal that would take the balance below -overdraft_limit raises ValueError and records nothing.
  • balance(): the sum of the ledger, as a Decimal.
  • transfer_to(other, amount): a withdrawal here plus a deposit there, all or nothing.
  • statement(): a list of (kind, amount, memo, balance_after) tuples in order, where kind is "deposit" or "withdrawal".
#classes#design#decimal2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Bank Account Ledger · TechGradify