It's Not All Animals and Fruits


<please somebody should help me explain this code in clear terms
see the body of the code below
if not product in self.items_in_cart:
self.items_in_cart[product] = price>

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."


if product in self.items_in_cart:

this will verify the item you want to remove is actually in your cart

self.items_in_cart[product] = price

items_in_cart is dictionary, this will add key (product) and value (price) pair to dictionary


hwy can i also write this code if not product in self.items_in_cart:, as if product not in self.items_in_cart:


there was never a not keyword in it?


please check this code in the original code above please (if not product in self.items_in_cart:)


i see, i mixed things up.

well, see if it works. Yea, given how conditions are evaluated both is possible, i would even prefer yours (not in is more logic given what you are doing)


ok thanks you,that other version took me long to interprete when reading that code


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