12. Stocking out


Can anybody explain me what am i doing wrong with this code?

def compute_bill(food):
    for item in food:
        if stock[item] > 0:
            total += prices[item]
    return total

this code return me errror:
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}


In line 6, you should have:

stock[item] = stock[item] -1

Or at least it worked for me!


i’ve changed as you said, but its still the same…


Oh, sorry to hear that!
My code looks like this:


It’s strange. the code is working, but it won’t to accept.
So, i will miss this lesson, and keep going


thats my code and it’s working


Remove line 26.

I have the same code as you, but I removed 26 and it worked for some reason


Hey! It’s worked! Thank you very much! You are great!


not working

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):
for n in food:
if stock[n]>0:
return total
Oops, try again. compute_bill([‘apple’]) returned 0 instead of 2


This is the same error I’m receiving. My code is identical to the examples above. What gives?


Well, I was able to get the module to accept my code, but I really don’t think it was right. It calculated the cost of buying every single banana, orange, and pear in the list. When I call “compute_bill(shopping_list),” it returns the right amount, but gives me an error message and won’t move on.


Does it have to do with white space?
total=0 is not the same as total = 0, etc…


Well, actually, I just tried it both ways and it worked. Please disregard


I did the same thing as you guys.
Just remove any thing extra like the print thingy.
Here’s my code: