'A Day at the Supermarket' giving me a weird error


#1


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


I checked the other threads and tested my code in IDLE. It should work, but I keep getting this error message:

Oops, try again. compute_bill(['apple']) resulted in a TypeError: list indices must be integers, not str

What am I doing wrong?


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


#2

food[item] is a string. Consider prices.


#3

So how do I convert it into a dictionary?


#4

You don't. There is a dictionary already, prices. Take the item from the food list, and look it up in the prices dictionary.


#5

How? I tried to do this in several ways and it's still not working.


#6

Notice that the keys in the prices dictionary are the same as the items in the food list? We use the items as keys when looking up the prices.

total += prices[item]

same as,

total = total + prices[item]

#7

Thank you very much!


#8

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