Its not all animal and fruits


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

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("john")                       
 my_cart.add_item("samina", 5)

i’m not sure what am i doing wrong here its kept giving me this error
[
Did you declare a variable called my_cart?
File “python”, line 23
my_cart = ShoppingCart(“john”)
my_cart.add_item(“samina”, 5)
^
SyntaxError: invalid syntax

<

```python

Replace this line with your code.

<do not remove the three backticks above>

#2

i get an IndentationError when running your code, can you verify your code is posted correctly on the forum?


#3

I’m posting it again here

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("john")                       
  my_cart.add_item("samina", 5)

#4

you create a class instance inside the class

instances need to be created after the class

classes are like blue prints, and instances are build of the blue print. You build something (instance) while the blue print isn’t finished yet (class)


#5

how do we know when the class is finished ??


#6

when you decide to finish it. You write the class, you have control

indention determines what is nested inside the class (just like everything else with python)


#7

okay so all this methods or functions are intended inside the class right ??


#8

yes, which is good

but creating the class instance is also indented to be inside the class, which shouldn’t be


#9

okay but what about the add_item statement shouldn’t it be intended inside the class ??


#10

No, given you call the method on the instance it should be outside the class.


#12

The code seems to run fine at least? haven’t run it in the exercise yet


#13

i’m not able to make progress because of this error if you could run it on exercise please


#14

What error? I ran your code:

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("john")                    
my_cart.add_item("samina", 5)

and it worked fine


#15

this error


#16


okay it worked thanks


#17

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.