Any Help Guys?


#1

class ShoppingCart(object):
"""Creates shopping cart objects
for users of our fine website."""
items_in_cart = {10}
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("ukelele",10)

Its given me error of stuffs I dont understand after I have made the indentation right...
Traceback (most recent call last):
File "python", line 24, in
File "python", line 11, in add_item
TypeError: 'set' object does not support item assignment


#2

Edit your post and fix your code so it is readable, highlight it and use the </> key above the text entry area or use markdown

Example:Markdown for Python

```python

# Code here

``` # end

After that I will try to assist you.


#3

class ShoppingCart(object):
"""Creates shopping cart objects for users of our fine website."""

#
items_in_cart = {10}
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("ukelele",10)

end

""" After making my adjustment it came back with this error below."""

python
Traceback (most recent call last):
File "python", line 24, in
File "python", line 11, in add_item
TypeError: 'set' object does not support item assignment


#4

Is that what is looks like?


#5

@teraninja07503

While this is not the solution is should help you grasp the concept that is presented. Take a gander at this codebit I have here and examine it. I have added comments on what everything does so it will be easy to decipher.

# You would store your price data in a database, this will substitute
products = ['Apple', 'Pear', 'Brick']
product_prices = {
    'Apple': 0.15, 'Pear': 0.25, 'Brick': 0.45
}


class ShoppingCart(object):
    """
    This is the shopping cart class which will allow us to store all the
    information we find important.
    """

    def __init__(self):
        """
        This will create these iems when we instantiate the class
        :return: None
        """
        self.storage = {}
        self.cart_owner = None
        self.cart_total = None

    def set_owner(self, owner):
        """
        This function sets the owner of the cart
        :param owner: Pass owners name
        :return: None
        """
        self.cart_owner = owner

    def add_cart(self, item, count):
        """
        This method adds the appropriate items to the cart, then calls the
        calc_total method to set the total price.
        :param item: Item to add
        :param count: Count of items to add
        :return: None
        """
        try:
            self.storage[item] += count
            self.calc_total()
        except KeyError:
            self.storage[item] = 0
            self.add_cart(item, count)

    def remove_item(self, item, count=1):
        """
        This method removes an item from the shopping cart down to 0
        :param item: Item to be deleted
        :param count: Count to be deleted, Default is one
        :return: String if input fails
        """
        try:
            if (self.storage[item] - count) > 0:
                self.storage[item] -= count
            elif (self.storage[item] - count) == 0:
                del self.storage[item]
            else:
                return 'There is not that many in the cart.'
        except KeyError:
            return '%s is not currently in the cart!'

    def calc_total(self):
        """
        This method computes the carts total price.
        :return: None
        """
        self.cart_total = sum((product_prices[item] * self.storage[item]) for item in self.storage)

    def __repr__(self):
        """
        This method allows us to call print on our class, giving us useful information.
        :return: String to be returned.
        """
        return "You currently owe %1.2f to the store %s" % (self.cart_total, self.cart_owner)
if __name__ == '__main__':
    # Here we instantiate a shopping cart to the variable joey
    joey = ShoppingCart()
    # The following lines set up our cart and owner
    joey.set_owner('Joey')
    joey.add_cart(products[0], 21)
    joey.add_cart(products[1], 4)
    joey.add_cart(products[2], 7)
    joey.remove_item(products[1], 1)
    # here we check on our cart.
    print(joey)

As you can clearly see having clear concise code make it readable, also you can see the intent quickly when it is designed like this. You can easily pick up these habbits if you keep at it.


#6

Many thanks for your assstance. I really appreciate @zeziba