Project: bank account
Python · Unit 12: Classes and objects
- Full plan
- Python
- Project
- 3 exercises
You're going to program a bank account. It stores its owner's name and its balance, which starts at 0.
You can deposit and withdraw, but never go below zero: if there isn't enough, the withdrawal doesn't happen and the balance stays the same.
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Complete the code
Complete
withdrawso it only takes the money out if there's enough. It should print 0.2. Write the code
Complete the
Accountclass:deposit(amount)adds to the balance, andwithdraw(amount)subtracts from it only if there's enough. Thesimulatefunction below is already written: it creates an account and applies the moves, where a positive number is a deposit and a negative one, a withdrawal.3. Write the code
Write
send(other, amount): if this account can withdraw the amount, deposit it intoother. If there isn't enough, neither account changes.simulatecreates Ana's and Leo's accounts, makes the transfer and returns both balances.
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.