Why does it work like this? 10.12


#1
def compute_bill(food):
    total = 0
    for item in food:
        if stock[item] > 0:
            total += prices[item]
            stock[item] -= 1
    return total

I got my code to work but can someone tell me why I need the '-=' as opposed to just a '-' in the sixth line.


#2

Hi @methodsurfer47887 ,

In the sixth line, you need to actually change the value associated with the item key in the stock dictionary. If you simply do this ...

stock[item] - 1

... you are computing a value that is 1 less than the current value that is stored there, but you are not changing the value that is stored there. To compute the new value and store it, you can do this ...

stock[item] = stock[item] - 1

... or this ...

stock[item] -= 1

#3

Thank you very much for that makes a lot more sense now.