Skip to content

Instantly share code, notes, and snippets.

@mhassanist
Created January 30, 2023 14:21
Show Gist options
  • Save mhassanist/8b3e730277dcf1298a6739e5ea000700 to your computer and use it in GitHub Desktop.
Save mhassanist/8b3e730277dcf1298a6739e5ea000700 to your computer and use it in GitHub Desktop.
class Product:
def __init__(self, name, price):
self.name = name
self.price = price
class Cart:
def __init__(self):
self.items = []
def add_item(self, product):
self.items.append(product)
def remove_item(self, product):
self.items.remove(product)
def total(self):
total = 0
for item in self.items:
total += item.price
return total
cart = Cart()
product1 = Product("Shampoo", 5.99)
product2 = Product("Toothpaste", 3.49)
cart.add_item(product1)
cart.add_item(product2)
print("Total: $", cart.total())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment