Classes
Create a BankAccount Class
Learning objective: By the end of this lesson, students will be able to create a class and instantiate objects from it.
🎓 You Do
Let’s get some practice building an object hierarchy in Python by creating a BankAccount class!
Create a BankAccount class with the following members:
owner: (attribute) The owner’s name as a stringbalance: (attribute) The amount of money in the accountaccount_no: (attribute) A number to be randomly generated and assigned within__init__- not passed in at time of instantiationdeposit(amount): (method) When called on an instance, increases thebalanceby theamountargument and returns the new balancewithdraw(amount): (method) When called on an instance, decreases thebalanceby theamountargument and returns the new balance
Create two instances, make both deposits and withdrawals, and print the attributes to test them out.
Hint
Here’s how to generate a random integer for the in Python:
import random
# Use this inside of BankAccount's __init__ to generate
# a random account number from 111111111 to 999999999
self.account_no = random.randint(111111111, 999999999)
Bonus
Override the __str__ method to return the following formatted string:
Account <account_no> - Balance: xxxxx.xx
Replacing <account_no> with the actual account number and xxxxx.xx with the actual balance.