10/18 Help


#1
indent preformatted text by 4 spaces

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

#2

go back one problem and run the same code under that problem, if it works with no console errors, then its just a case of the lesson being picky, and in that case just make sure all of the exact requirements of the problem are met.


#3

I have checked it on the last problem; however, it still doesn't work. I also checked all the requirements and I still can't figure it out. Do you have any other ideas?


#4

I haven't gotten to this problem yet, and I don't see anything wrong.
What does self.customer_name = customer_name and self.items_in_cart[product] = price do?
also what error do you get if any? If you don't get an error, I don't see a return so maybe that?


#5

I was able to get my code to work once I fixed the indent on the last part.

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

my_cart = ShoppingCart("Eric")
my_cart.add_item("Train", 10)