-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path03_instance_methods.py
More file actions
26 lines (23 loc) · 839 Bytes
/
Copy path03_instance_methods.py
File metadata and controls
26 lines (23 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class BankAccount:
"""
Class representing a Bank Account with deposit and withdraw methods.
"""
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amount):
"""Adds money to the account balance."""
self.balance += amount
print(f"Deposited ${amount}. New balance: ${self.balance}")
def withdraw(self, amount):
"""Subtracts money from the account balance if funds are available."""
if amount <= self.balance:
self.balance -= amount
print(f"Withdrew ${amount}. New balance: ${self.balance}")
else:
print("Insufficient funds!")
# Working with instance methods
account = BankAccount("Krish Sharma", 1000)
account.deposit(500)
account.withdraw(200)
account.withdraw(2000)