Struggling to understand whats going on here. Shopping cart


#1

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."
    else:
      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."
    else:
      print product + " is not in the cart."



#2

the init method has been explained:

https://www.codecademy.com/courses/learn-python/lessons/introduction-to-classes/exercises/more-on-init-and-self?action=lesson_resume

methods have been explained:

https://www.codecademy.com/courses/learn-python/lessons/introduction-to-classes/exercises/a-methodical-approach?action=lesson_resume

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


#3

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