The instructions for this exercise say:
“Make the following changes to your compute_bill function:
While you loop through each item of food, only add the price of the item to total if the item’s stock count is greater than zero.
If the item is in stock and after you add the price to the total, subtract one from the item’s stock count.”
But when I run my code, it says this:
“Oops, try again. stock doesn’t look quite right! Make sure to not call compute_bill since it changes the stock! It should contain: {‘orange’: 32, ‘pear’: 15, ‘banana’: 6, ‘apple’: 0}”
It’s saying I shouldn’t change the stock? But the instructions tell me to reduce the stock by one, so I’m confused about what is going wrong here.
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
}
def compute_bill(food):
total = 0
for item in food:
if stock[item] > 0:
total += prices[item]
return total
for product in shopping_list:
if stock[product] > 0:
stock[product] -= 1
total = compute_bill(shopping_list)
print total
GAH I’ve been banging my head against the wall with this one for about a half hour now. This irritates me that the issue was it didn’t want me actually calling the function - what the ■■■■ right?