11. Making a purchase - code works but wrong answer


Here is my code:

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

The code is correct and it says that I completed it, however it printed out 117 which means it calculated that I bought all the fruit in stock, including the pears. What did I do incorrectly?


Is this the lesson that says to ignore this discrepancy?


It states:

"Ignore whether or not the item you're billing for is in stock.

Note that your function should work for any food list."

So the apple is not in stock, but my code should not have included pear since it isn't on the shopping list. Also seems odd that all the fruit would be bought, not just one of each.


Try calling the function on the grocery list...

print compute_bill(shopping_list)


That works, thank you so much!