Making a Purchase


#1



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


I have followed all the instructions but the error that pops is:
"Oops, try again. compute_bill(['apple']) returned 10.5 instead of 2"


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 prices:
        total += prices[food]
    return total


#2

Hi @danyyoo,

The instructions include the following ...

For each item in the food list, add the price of that item to total.

However, here, you are iterating through the items in the prices dictionary instead of through the items in the food list ...

    for food in prices:
        total += prices[food]

#3

But there is no food list. With the:

for food in prices: total += prices[food]

I am adding the prices of the foods to the total


#4

The function parameter is food. When the function is called, a list will be passed to the function as an argument, and a reference to that list will be assigned to food.

To iterate through food, change this loop header ...

   for food in prices:

... to something like ...

    for item in food:

Then inside the loop you can use item as a key to access the prices dictionary.


#5

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