Output is correct, but I keep on getting this message: compute_bill(['apple']) resulted in a TypeError: list indices must be integers, not str


#1


shopping_list = ['banana', 'orange', 'apple']

stock = {
    'banana': 6,
    'apple': 0,
    'orange': 32,
    'pear': 15
}
    
prices = {'orange': 1.5, 'pear': 3, 'banana': 4, 'apple': 2
}

# Write your code below!
def compute_bill(food):
    total = 0
    for key in food:
        total += food[key]
    return total

print compute_bill(prices)


#2

Hi I think that you don't need to call the function compute_bill(), in this lesson so try to remove it.

print compute_bill(prices)

#3

still got the same message


#4

instead of

total += food[key]

put

total += prices[key]

#5

okay, that solved the problem, thank you!!! but I'm confused, are we writing the function for any list? why are we hard coding the prices list?


#6

That was indeed confusing. They were asking us to use a list that wasn't part of the program instead of just using the directories we already made. :confused:


#7

Hi this part of the instruction

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

so with the for loop we go throught each item in the food list then we add the price of the item inisde the total and then we return it.

when you will cal the function compute_bill() with a list of item it will print the total of the price of all the item inside the list .

you could use the shoppingg_list.
Hope that could help you understand it


#8

This is indeed very confusing.
I ran into exactly the same problem.
Why call for prices instead of food[item]? Isn't prices a 'global' dictionary?


#9

It is but you must get the price of each food item. The only way to do so is going through the price dictionary for each food item and adding the amount.