Why I get : Oops, try again. compute_bill(['apple']) returned 7.5 instead of 2


#1

This is my code:

def compute_bill(food):
    total =0
    for item in shopping_list:
        total += prices[item]
    return total
    


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

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

# Write your code below!  
print compute_bill(shopping_list)

the error message is: Oops, try again. compute_bill(['apple']) returned 7.5 instead of 2


#2

you loop over shopping_list, this means you function only works for a single list. It should work for any lists supplied as argument on function call, to achieve this, loop over the function parameter


#3

But apple stock is =0 (so you can't buy it :slight_smile: )
Thanks - it helped me resolve the problem!


#4

you will work on this issue in the next exercise, doing everything in one go can be very confusing, the exercise goes in steps


#6

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