Class representing a bank account with read-only balance
I'd like you to make a class that represents a bank account.
Your bank account should accept an optional balance
argument (defaulting to 0), have a balance
attribute, and have deposit
, withdraw
, and transfer
methods:
>>> a1 = BankAccount()
>>> a1.balance
0
>>> a1.deposit(10)
>>> a1.balance
10
>>> a2 = BankAccount(balance=20)
>>> a2.withdraw(15)
>>> a2.balance
5
>>> a1.transfer(a2, 3)
>>> a1
BankAccount(balance=7)
>>> a2
BankAccount(balance=8)
Your bank account should also have a nice string representation (as shown above).
This example might not seem realistic (it's not) but it's a classic example for good reason:
Bonus 1
This is just a preview of the problem statement.
This exercise includes 3 bonuses and 7 hint links.
To solve this exercise, sign in to your Python Morsels account.
My name is Trey Hunner and I hold Python trainings for teams. I've spent countless hours refining my most interesting Python exercises into Python Morsels, a Python skill-building platform for folks who already know Python. Python Morsels is design to help you deepen your Python skills every week.
Sign up for Python Morsels to access this exercise and many more.
Join Python Morsels ✨