Editor

A Bank Account Class

Intermediate · Write the code · from 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

From the lesson: 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

Fifty problems, each with a code editor and real tests. Write your solution, press Check, and see exactly which cases pass. Stuck? Hints reveal one at a time, and the solution unlocks once you have checked at least once. Your code and progress are saved in this browser.

Read the lesson

Write a class BankAccount:

  • BankAccount(owner, balance=0)
  • deposit(amount) and withdraw(amount). Both raise ValueError for amounts of 0 or less, and withdraw raises ValueError if there is not enough money.
  • a read-only balance property
  • history is a list of ("deposit", amount) / ("withdraw", amount) tuples for successful operations
  • class BankAccount:
    def __init__(self, owner, balance=0):
    pass
    Solution unlocks after your first Check

    What Check runs

    Your code runs first, then each of these lines. The output must match exactly.

    RunsExpected output
    acct = BankAccount("Ana", 100) acct.deposit(50) acct.withdraw(30) print(acct.balance, acct.history)120 [('deposit', 50), ('withdraw', 30)]
    acct = BankAccount("Ben") try: acct.withdraw(10) except ValueError: print("refused", acct.balance, acct.history)refused 0 []

    More practice problems

    All Python exercises · 50 practice problems