Struggling to understand whats going on here. Shopping cart


Is it just me of has none of this been explained? If someone is up for it, please explain whats going on here?

class ShoppingCart(object):
  """Creates shopping cart objects
  for users of our fine website."""
  items_in_cart = {}
  def __init__(self, customer_name):
    self.customer_name = customer_name

  def add_item(self, product, price):
    """Add product to the cart."""
    if not product in self.items_in_cart:
      self.items_in_cart[product] = price
      print product + " added."
      print product + " is already in the cart."

  def remove_item(self, product):
    """Remove product from the cart."""
    if product in self.items_in_cart:
      del self.items_in_cart[product]
      print product + " removed."
      print product + " is not in the cart."


the init method has been explained:

methods have been explained:

most of this has been explained actually, but seeing altogether might be a bit overwhelming

we have a class, which has a member variable named items_in_cart which keeps track of the items in shopping_cart. Just like we had is_alive member variable earlier, only this time its a dictionary, not a boolean value like is_alive

then we have methods which can add or remove items from our shopping_cart


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.