Stocking Out : Error


#1

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

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

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

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

this it the error: Oops, try again. calling compute_bill with a list containing 2 pears, 3 oranges and 8 bananas resulted in 42.5 instead of the correct 34.5 , please help, thank you.


#2

As indentation is not clear here ,The problem might be the last statement that is ..
return total

Make sure its indented well!

Things to take care here,

for loop is block inside the function so It should be indented by 4 spaces w.r.t function.
if- block is inside of loop so it should be indented by 4 spaces w.r.t to for loop.
as I mentioned that our return statement is final result so It should be separate block too.


#3

It's working now, thank you.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.