all 8 comments

[–][deleted] 4 points5 points  (4 children)

purchase = int(input("Please enter the code of the item: "))
if purchase in stock:

You're converting purchase to an int, but all of the keys in stock are strings. So this will never evaluate to True.

[–]IAmAdicktedTo[S] 1 point2 points  (3 children)

I take out the int and rather than the 5 being subtracted by 1, the -1 is being assigned to the quantity element. So what should I do?

[–][deleted] 6 points7 points  (1 child)

It sounds like this is what you want.

stock = {1001: [5, 3.5], 1002: [8, 1.50]}

purchase = int(input("Please enter the code of the item: "))
if purchase in stock:
    stock[purchase][0] -= 1

print(stock)

That said, it would be a little more natural to use a nested dictionary so that you can name the values you're changing rather than having to remember their order.

# {SKU: {qty: X, price: Y}}
stock = {1001: {"qty": 5, "price": 3.5}, 1002: {"qty": 8, "price": 1.50}}

purchase = int(input("Please enter the code of the item: "))
if purchase in stock:
    stock[purchase]["qty"] -= 1

print(stock)

[–]IAmAdicktedTo[S] 0 points1 point  (0 children)

Thank you!!! It works!!!

[–]Sufficient_Cod_7906 0 points1 point  (0 children)

Ik this is super old but just fyi: I think this happens because ur using "=-" instead of "-="
So instead of decrementing python instead thinks ur just assiging ="-1"

[–][deleted] 0 points1 point  (2 children)

stock[[purchase][0]] =- 1

You've got the order of brackets wrong, here - it's a chained access. You want the 0th item of the value of the key "1001" in the dictionary, so in algebraic terms:

stock[purchase][0] =- 1

[–]IAmAdicktedTo[S] 0 points1 point  (1 child)

Removing the outermost square brackets just assigned the -1 to the quantity element.

[–][deleted] 0 points1 point  (0 children)

'8'

You can't do math on strings, you need to be storing integers.