It's not all Animals and Fruits additional fun


#1

After this exercise I decided that a shopping cart is nice, but it should also include the option of getting a total for when you're finished shopping. I added the total function in as you can see below. Has anyone else made any improvements to their shopping carts?

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."
    def total(self):
        total = 0
        for i in self.items_in_cart:
            total += self.items_in_cart[i]
        print "Thank you %s, your total today is: %s" % (self.customer_name, total)

my_cart = ShoppingCart("James")
my_cart.add_item("Cheese Cake", 30)
my_cart.add_item("Fruit", 10)
my_cart.add_item("Veggies", 5)
my_cart.add_item("Craft Supplies", 50)
print my_cart.items_in_cart
print my_cart.total()