11/13 Why doesn't this work?


#1


https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096

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):
    total=0
    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?


#2

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


#3

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


#4

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


#5

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?


#6

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


#7

This code replace:

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

#8

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