For this challenge, create a bank account class that has two attributes:
and two methods:
As an added requirement, withdrawals may not exceed the available balance.
Instantiate your class, make several deposits and withdrawals, and test to make sure the account can't be overdrawn.
class Account:
def __init__(self, owner, balance):
self.owner = owner
self.balance = balance
def __str__(self):
return f"Account Owner: {self.owner}\nAccount Balance: {self.balance}"
def deposit(self, amount):
if amount <= 0:
print('Deposits must be greater than 0.')
else:
self.balance += amount
print("Deposit Accepted")
def withdraw(self, amount):
if amount <= 0:
print('Withdrawls must be greater than 0.')
elif self.balance - amount < 0:
print('Funds Unavailable!')
else:
self.balance -= amount
print('Withdrawal Accepted')
# 1. Instantiate the class
acct1 = Account('Jose',100)
# 2. Print the object
print(acct1)
Account Owner: Jose Account Balance: 100
# 3. Show the account owner attribute
acct1.owner
'Jose'
# 4. Show the account balance attribute
acct1.balance
100
# 5. Make a series of deposits and withdrawals
acct1.deposit(-50)
Deposits must be greater than 0.
acct1.withdraw(-75)
Withdrawls must be greater than 0.
# 6. Make a withdrawal that exceeds the available balance
acct1.withdraw(500)
Funds Unavailable!