Code not working for 11 Making a Purchase


#1



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)


#2

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


#3

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


#4

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