Question: Class Constructor def _ _ init _ _ ( self , account _ number, balance = 0 ) : self.account _ number = account _

Class Constructor
def __init__(self, account_number, balance=0): self.account_number = account_number self.balance = balance
Creates a new bank account with:
Required account number
Optional starting balance (defaults to $0)
2. Deposit Method
def deposit(self, amount): if amount >0: 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
3. Withdraw Method
def withdraw(self, amount): if 0< 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
4. Balance Check Method
def get_balance(self): return self.balance
Simply returns the current account balance
5. Test Code
if __name__=="__main__": account = BankAccount("1234567890") account.deposit(100) account.withdraw(50) if not account.withdraw(100): print("Invalid withdrawal amount")
Test sequence:
Creates account #1234567890
Deposits $100
Withdraws $50
Attempts to withdraw $100(fails due to insufficient funds)
Step 3: 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, deposit(self, 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 (negative/zero), returns False without changing the balance
Add the deposit(self, amount)method to your BankAccount class after the existing methods:
def add_interest(self, rate): # Convert percentage to decimal (5%->0.05) interest_rate = rate /100 # Calculate interest amount interest_amount = self.balance * interest_rate # Add interest to balance self.balance += interest_amount
2. 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!