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
- A new account stores owner and balance
- The balance defaults to 0
deposit(25)returns the new balancewithdrawtoo much raises ValueError and keeps the balance- Withdrawing exactly the whole balance is allowed
- Two accounts keep separate balances
- 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.