0

I have been working on a python problem for the past four days and I keep getting a small problem when I run my code. The error that I am getting is the following:

Traceback (most recent call last): File "cuddy_m_chap10_7.py", line 163, in main() File "cuddy_m_chap10_7.py", line 121, in main cr1.clear(r3) File "cuddy_m_chap10_7.py", line 48, in clear self.__list_item.remove(r) ValueError: list.remove(x): x not in list

Now, I have researched this problem but cannot find anything that helps me. To start, I have a simple program that has three objects, which represent clothes. I have a cash register class and here are some of its methods:

  def __init__(self):
     self.__list_item = []

  def purchase_item(self, r):
     self.__list_item.append(r)

  def clear(self, r):
     for item in self.__list_item:
        self.__list_item.remove(r)

As the program runs, the user may add clothes to a shopping cart or take clothes out. Now, if I add two pieces of clothing, I can use the clear method and take out a piece. However, if I have three pieces of clothing in the cart then get the trace back error. So, 2 items in cart, everything works fine, three or more pieces I get my error. Here is the code is some of the main function code:

  #Creating the objects from the class
  r1 = RetailItem('Jacket', 12, 59.95)
  r2 = RetailItem('Designer Jeans', 40, 34.95)
  r3 = RetailItem('Shirt', 20, 24.95)

  #creating cash register object
  cr1 = CashRegister()

  print("Listing All Items:")
  print('ITEM', '\t', '\t', '\t', 'PRICE')
  print(r1.get_desc(), '\t', '\t', '\t', r1.get_price())
  print(r2.get_desc(), '\t', '\t',  r2.get_price())
  print(r3.get_desc(), '\t', '\t', '\t', r3.get_price())

  #Asking the user which items they want to buy
  ch = 'y'
  while ch.upper() == 'Y':
    print()
    print("Please enter the name of the item to be purchased")
    item = input('Name: ')
    if item.upper() == r1.get_desc().upper():
      cr1.purchase_item(r1)
    elif item.upper() == r2.get_desc().upper():
      cr1.purchase_item(r2)
    elif item.upper() == r3.get_desc().upper():
      cr1.purchase_item(r3)
    ch = input('Select another item: (y/n): ')

  #Displaying a list of the items which have been placed in the cart
  print()
  print("The list of the items you have in your cart are as follows: ")
  print(cr1.show_items())
  print()

  remove = input("Do you want to remove an item? (y/n) ")
  while remove.lower() == 'y':
    if remove.lower() == 'y':
      print("Please enter the name of the item you want to be remove: ")
      name = input('Item: ')
      if name.upper() == r1.get_desc().upper():
        cr1.clear(r1)
        print("The", r1.get_desc(), "has been removed!")
      elif name.upper() == r2.get_desc().upper():
        cr1.clear(r2)
        print("The", r2.get_desc(), "has been removed!")
      elif name.upper() == r3.get_desc().upper():
        cr1.clear(r3)
        print("The", r3.get_desc(), "has been removed!")
      remove = input("Do you want to remove an item? (y/n) ")

any help on why my program works with 2 items in the cart but not 3 will be greatly appreciated! Thank you

Erik Tena
  • 5
  • 2
  • Don't delete from a list while iterating over it.. The iteration uses indices, and when you delete from the list those indices *no longer are accurate*. – Martijn Pieters Dec 08 '16 at 14:51

1 Answers1

0

You should not be using a for loop at all. Just remove that one item:

def clear(self, r):
    self.__list_item.remove(r)

You are trying to remove r from the list as many times as you have total items in the list..

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Oh wow, such an easy solution! I cannot believe that I messed that up and understand what I was doing wrong. Thank you with the extra set of eyes! – Erik Tena Dec 08 '16 at 14:57