Question: Class Constructor def _ _ init _ _ ( self , account _ number, balance = 0 ) : self.account _ number = account _
Class Constructor
def initself accountnumber, balance: self.accountnumber accountnumber self.balance balance
Creates a new bank account with:
Required account number
Optional starting balance defaults to $
Deposit Method
def depositself amount: if amount : self.balance amount return True return False
Checks if amount is positive
Adds amount to balance if valid
Returns True for success, False for invalid amount
Withdraw Method
def withdrawself amount: if amount self.balance: self.balance amount return True return False
Verifies amount is positive and not more than balance
Subtracts amount if valid
Returns True for success, False if invalid or insufficient funds
Balance Check Method
def getbalanceself: return self.balance
Simply returns the current account balance
Test Code
if namemain: account BankAccount account.deposit account.withdraw if not account.withdraw: printInvalid withdrawal amount"
Test sequence:
Creates account #
Deposits $
Withdraws $
Attempts to withdraw $fails due to insufficient funds
Step : Add a New Method
A method is a function that belongs to a class and defines an action that objects of that class can perform. In this code, depositself amount is a method that:
Takes a money amount as inputChecks if the amount is positiveIf valid, adds that amount to the account's balance and returns TrueIf invalid negativezero returns False without changing the balance
Add the depositself amountmethod to your BankAccount class after the existing methods:
def addinterestself rate: # Convert percentage to decimal interestrate rate # Calculate interest amount interestamount self.balance interestrate # Add interest to balance self.balance interestamount
Run the code again to ensure your new method works correctly. Remember to test your code thoroughly before submitting!
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
