10 Not all fruits and animals but I'm going bananas!


#1

I'm not sure where my error is. I have tried what seems like a million different ways and none of them run!

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 the 1/2 bee")    
    my_cart.add_item = ("Eric the fruit bat" , 8)

#2

Well at the moment you're not actually running anything. All you've done is create a class.
You need to call that class to run (using print or return)


#3

Wouldn't it function better if you had a dict of the object with it's price then your cart could just be a dict with the object and it's quantity.

products = {
    'apple': 1.25,
    'pear': 61.42
}
class customer(object):
    def __init__(self):
        self.cart = dict()

    def what_in_cart(self):
        return ''.join({Item: Count}) for Item, Count in self.cart.items())

   def add_item(self, item, quantity=0):
       self.cart[item] = quantity

    def remove_item(self, item, quantity=0):
        if (self.cart[item] - quantity) > 0:
            self.cart[item] -= quantity
        else:
            del self.cart[item]

Something along those lines should work better. You want to keep a dedicated list of products and then have a customer class/object that has all the things you need to work on it with.

You would use it like,

al = customer()
al.add_item('apple', 4)
print(al.what_in_cart())

You can add error checking easily enough if your base code it good.

Any questions ask freely.


#4

But when I hit "Save & Submit Code" I get the error Oops, try again. Did you declare a variable called my_cart?


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

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