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



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


food[item] is a string. Consider prices.


So how do I convert it into a dictionary?


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


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


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]


Thank you very much!


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