0

the question which I am working on is this: A program for a supermarket, that checks whether a product is available or not, and then bills the product. If the product ID is present in the list of product IDs, you can add the product to the bill. Once the product is sold, you will decrease the number of products in stock.

product = ["apple", "pear", "banana", "orange", "guava"]
productID = ["a", "p", "b", "o", "g"]
productLeft = [3, 3, 3, 3, 3]

buy = str(input("enter product ID"))
if any(buy in s for s in productID):
    num = int(productID.index(buy))
    left = productID[num]
if left != 0 :
    left = left-1
    productLeft[num] = left

I'm getting this error message:

  left = left-1   
TypeError: unsupported operand type(s) for -: 'str' and 'int'

and if do this:

left = int(productID[num])

then I get this error message:

  left = int(productID[num])   
ValueError: invalid literal for int() with base 10: 'a'

Please help what correction should I make in my current code and how I should proceed

1
  • Instead of 3 separate lists, you should have a single dict that maps a product ID to its name and quantity, perhaps something like {"a": ["apple", 3], "p": ["pear", 3]}. Then you can get the name or quantity with products["a"][0] or products["a"][1], respectively. Commented Apr 2, 2018 at 14:36

1 Answer 1

2

You are trying to treat the product ID as the number of items that are left. Don't use productID, use the value from productLeft:

left = productLeft[num]

Instead of using lists, and having to search for the product ID each time, use some dictionaries:

products = {"a": "apple", "p": "pear", "b": "banana", "o": "orange", "guava"}
productLeft = {"a": 3, "p": 3, "b": 3, "o": 3, "g": 3}

and

buy = input("enter product ID")
if buy in products:  # valid existing key
    if productLeft[buy] > 0:
        productLeft[buy] -= 1
    else:
        print("There is nothing left")
else:
    print("Sorry, there is no such product")
Sign up to request clarification or add additional context in comments.

Comments

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.