1

I am trying to make a programm where I have a shop. I have a stock, need to add objects to a receipt, remove objects from a receipt and print the receipt. I have managed to do everyting but my delete method is doing some mistakes.

def deleteItem(self,item,productAmount):

    i = 1
    for product in self.receipt:
        if item == product.code:
            if product.amount >= productAmount:
                while i <= productAmount:                            
                    self.receipt.remove(product)
                    i+=1
            else:                                                   
                print("Amount is too large, there are ",product.amount," left")
        else:
            print("product code is not in the order")

Whenever I have assigned the item variable to be different than the product.code then the messasge is printed as many times as the product.amount is in the receipt (which is a list). I understand this is probably happening because of the "for product in self.receipt", but i dont know any other way of refering to that list. Also whenever I choose a productAmount > product.amount then my programm uses the ValueError I have used in my menu function:

def menu():

loadProducts()
while True:
    print("\nWhat do you want to do?")
    print("1. Scan object")
    print("2. Show order")
    print("3. Print receipt")
    print("4. Show stock")
    print("5. delete product")

    try:
        choice = int(input())
        if choice == 1:
            scanProduct()
        elif choice == 2:
            lager.showOrder()
        elif choice == 3:
            lager.printReceipt()            
        elif choice == 4:
            print(lager)
        elif choice == 5:
            removeItem()
        else:
            print("You must choose a number between 1 - 5")
    except ValueError:
        print("You must choose a number between 1 - 5")

Thank you in advance and if I have not been clear enough I apologise and I am here for clarifications. :)

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
dimitris
  • 11
  • 2

0 Answers0