It's Not All Animals and Fruits


#1


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

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


#3

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


#4

there was never a not keyword in it?


#5

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


#6

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)


#7

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


#8

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