A Safe Bank Account
Python
20 pts
A Safe Bank Account
Create a class BankAccount that keeps its money in a private attribute __balance:
__init__(self, owner, balance=0)storesself.ownerandself.__balance.deposit(amount)adds the amount only if it is greater than 0.withdraw(amount)subtracts the amount if there is enough money; otherwise it printsNot enough moneyand changes nothing.get_balance()returns the balance.
The code at the bottom is ready. Expected output:
Not enough money 1200
Checklist
- Define a class
BankAccountthat stores the owner and keeps the balance (default 0) in the private attribute__balance. - Add
get_balance(), which returns the balance. - Add
deposit(amount), which adds the amount only if it is greater than 0. - Add
withdraw(amount), which subtracts the amount if there is enough money and otherwise printsNot enough money. - Run the code at the bottom.
🔑 Solution
If you view the solution before passing, you earn half points for this exercise. You still need to type it and press Check to finish.
How it works, step by step
Point at a step to highlight its lines. Type the solution yourself and press Check answer to finish.
