Command Palette

Search for a command to run...

Hectal
Phase 8: Object-Oriented Python
Python · Exercise 1 of 6Next
Beginner

Build a BankAccount class

Write a class BankAccount with __init__(self, owner, balance=0), storing owner and balance.

Add deposit(amount) which adds to the balance and returns the new balance, and withdraw(amount) which subtracts it. If the amount is more than the balance, withdraw raises ValueError and leaves the balance unchanged.

Create an account for "Asha" with 100, withdraw 30, and print Balance: 70.

What Check tests

  1. A new account stores owner and balance
  2. The balance defaults to 0
  3. deposit(25) returns the new balance
  4. withdraw too much raises ValueError and keeps the balance
  5. Withdrawing exactly the whole balance is allowed
  6. Two accounts keep separate balances
  7. The program prints Balance: 70

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.