0

messing around with Python and was building a simple text cart thing. when i try to iterate through the cart, using the defined __str__, it give me this error: 'CartItem' object is not subscriptable. i've tried to understand what this was talking about, but have failed to find anything on the matter.

here is the relevant code:

class CartItem:
  def __init__(self, name, price):
    self.name = name
    self.price = price
  def __str__(self):
    print(self.name + " --> " + self.price)
    return "-- item printed --\n"

class Cart:
  def __init__(self):
    self.items = []

  def __str__(self):
    if(len(self.items) > 0):
      index = 0
      for item in self.items:
        print(str(index) + ": " + item[index].name + ' --> ' + item[index].price)
        index += 1
    else:
      print("No Items in Cart")
    return "-- cart items printed--\n"

  def addItem(self, cartItem):
    self.items.append(cartItem)
    print (cartItem.name + ", costing $" + cartItem.price + ", has been added to cart\n")
    print(self.items)  # *should* print out the contents of the cart, but doesn't...

flarn = CartItem("flarn", "200.00")
print(flarn)
userCart = Cart()
userCart.addItem(flarn)
print(userCart)  # if i remove this line, i get [<__main__.CartItem object at 0x....>]??

can someone please explain what that error, 'CartItem' object is not subscriptable, means in context of this code? bonus point if you can explain the output removing print(userCart) returns.

1
  • 1
    You're both looping over and indexing items in the same loop, item[index].name. You don't need index and item is an item from the list you're looping over, not the list itself. Commented Nov 16, 2021 at 19:04

1 Answer 1

3

The problem is item[index].name - CartItem is not subscriptable as the error states. You don't need to use index, you iterate over list. Also you can simplify your __str__() method.

Note that using print() inside the __str__() method is bad. Better construct a string to describe the cart and return it. Also should have __repr__ instead of __str__() for CartItem. It will be used when you print items in container like list Cart.items. Also you can use the string representation of the Item when you construct the Cart.__str__()

class CartItem:
    def __init__(self, name, price):
        self.name = name
        self.price = price
    def __repr__(self):
        return f'{self.name} --> {self.price}'


class Cart:
    def __init__(self):
        self.items = []

    def __str__(self):
        if self.items:
            return '\n'.join(f'{idx}: {item}' for idx, item in enumerate(self.items))
        return "Empty Cart"

    def addItem(self, cartItem):
        self.items.append(cartItem)
        print(f'{cartItem.name}, costing ${cartItem.price}, has been added to cart')
        print(self.items)  # *should* print out the contents of the cart, but doesn't...


userCart = Cart()
print(userCart)
flarn = CartItem("flarn", "200.00")
print(flarn)
userCart.addItem(flarn)
flarn = CartItem("flarn", "100.00")
print(flarn)
userCart.addItem(flarn)
print(userCart)

output:

Empty Cart
flarn --> 200.00
flarn, costing $200.00, has been added to cart
[flarn --> 200.00]
flarn --> 100.00
flarn, costing $100.00, has been added to cart
[flarn --> 200.00, flarn --> 100.00]
0: flarn --> 200.00
1: flarn --> 100.00
Sign up to request clarification or add additional context in comments.

4 Comments

fantastic! thank you. i learned so much from this. wow. i just have one remaining question: what the heck does the error actually mean?? can't find anything on subscriptable...
also, the singe line ``for` loop was handy as heck! thanks again. :)
that is generator expression. Also have a look at e.g. list comprehension. There are also dict comprehension and set comprehension

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.