A Day at the Supermarket - Stocking Out


Here's my code. I don't know what is wrong. Please help.

food = ["banana", "apple", "orange", "pear"]

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15

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


Sorry, I did the correct indentation. Something went wrong with the paste. The indentation is not the problem. Here's the picture of the code.



It would be less confusing if the variable outside of the function had a different name than food. That is the locally defined variable. Give your list a name like, shopping_list, then pass it to the function with,


None of these lines is asked for or expected. We shouldn't be modifying the shopping_list, That only confuses matters further.

    for item in food:
        if stock[item] > 0:
            total += prices[item]
            stock[item] -= 1