Code not working for 11 Making a Purchase


My code is throwing an error. Could you help me out?

10. A Day at the Supermarket, 11. Making a Purchase

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

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
print compute_bill(stock)


this line:

total = total + food[item]

where are the prices of the items stored? Not in food, but in your prices dictionary, try using item to access the price in your prices dictionary


Thanks! I thought it wanted the function to be usable for both dictionaries.


the keys for both dictionary's are the same? so in your case item can be used to both access the prices and stock, which is what you are going to do later. Because you want to build something which will bill people if they buy something, but also reduce stock, otherwise you run out of stock without knowing it

But that is for later