11/13 Why doesn't this work?



Oops, try again. compute_bill(['apple']) returned 4 instead of 2

return the total

Replace this line with your code. 

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!
def compute_bill(food):
    for food in shopping_list:
        total=total + prices[food]
        return total

According to my logic, im looking for "food" in the "shopping_list", and taking the prices of "food" from the prices list and adding them to the total. Why is this returning incorrectly for me?


Indentation is very important. Make sure your return statement is indented correctly.


While indentation is helpful in making your code legible, it doesn't have any bearing on your code's functioning.


You want to loop through the items in the food parameter instead of looping through your shopping_list.


I don't understand the logic there though. From my point of view what im doing is taking everything in the shopping_list and referencing it to everything in prices. Then taking those values and adding it to the total. How is this wrong?


They want you to pass the shopping_list in as an argument to compute_bill instead of referencing it directly in the function.


This code replace:

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


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