10. It's not all animals and fruits


#1



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 my_cart(item, quantity):
        my_cart= ShoppingCart("food")
        my_cart.add_item("apple", 1)
    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."


keeps asking me if I've declared a variable called my_cart, I'm not sure what this activity wants me to do.


#2

You don't create an object inside of the declaration...

You need to create it with the constructor. :slight_smile:


#3

Does that mean I insert new code at the end of the code that was already there, instead of in the middle?


#4

Yes, don't declare the variable inside of the function.


#5
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 my_cart(item, quantity):
        my_cart= ShoppingCart("food")
        my_cart.add_item("apple", 1)

It's still asking me if I declared a variable.


#6

You don't have to alter the code that is already provided for this exercise. You must simply create the new object that belongs to the shopping cart class. The easiest way to proceed from here would be to click the "Reset Code" button at the bottom by "Save & Submit Code". Then your screen should look like this:

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

then you want to create your new object using your class which will look something like object_name = ClassName(variable, variable)
in this exercise the object_name is my_cart and the ClassName is ShoppingCart with only a single variable of (customer_name)

Below is the answer but you should try to figure it out once more before scrolling down.
.
.
.
.

Try it yourself first, answer below


.
.

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("James")
my_cart.add_item("Cheese Cake", 15)

#7

Thank you I must have figured this out somehow because I finished the class. I probably should have posted that on this thread I just forgot about it.


#8

That's alright, I didn't notice how old the original thread was. Well maybe it will help others with the same problem :smile: