Stocking Out - seems checking for stock[item] > 0 is insufficient


#1

Hello all! I'm stuck on 12 - Stocking Out. It says to only check that the stock[item] is greater than zero, but the code is failing and I believe it's because I'm computing a total for 8 bananas, but I only have 6 in stock. Not sure how to resolve it. Thanks for any help!

I receive this error...
Oops, try again. calling compute_bill with a list containing 1 apple, 2 pears, 1 orange and 8 bananas resulted in 39.5 instead of the correct 31.5

Here is my code...

shopping_list = ["apple", "orange", "banana"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below!
def compute_bill(food):
    total = 0
    for item in food:
        if stock[item] > 0:
            total += prices[item]
            stock[item] - 1
        else:
            total += 0
    return total

#2

this line:

stock[item] - 1

doesn't reduce your stock, you do it right here:

total += prices[item]

which is a shorthand for:

total = total + prices[item]

but then when you have to reduce the stock, you don't update the variable


#3

Thanks for your reply! I'm afraid I don't understand.

Are you saying I shouldn't include the line with stock[item] - 1? Or are you saying I need that line, I just don't have it correct?


#4

Oh! I just got what you meant. I needed "stock[item] -= 1".

Thank you!!


#5