Stocking Out


#1

Hi, I have tried this exercise for hours and I cant figure out where my mistake is. I hope can someone at least points out my mistake?
def compute_bill (food) :
total = 0
for key in food :
if stock [key] > 0 :
total = total + prices [key]
stock [key] = stock [key] - 1
return total

food = shopping_list
print compute_bill(food)

The error shown is "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 "

Thank you


#2

You pretty much have the answer in plain text from the error message actually. Remove the last line.

For some reason, the tests Codecademy runs on your code does not reset the stock list before starting the testing, so you can't call the function in your code. Alternatively I'm guessing that you could reset the stock list yourself on your last line if you want to test the function. It's a bit stupid and leads to a lot of pointless confusion.


#3

The stock and prices dictionaries should still be in your code.

Also you only need to define the compute_bill() function, not call it. You call the function in your last line.


#4

Thanks, I will check that later.


#5

Thanks! I was stuck here too...


#6

this is my code..hope will help..

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 key in food:
        if stock[key] > 0:
            total = total + prices[key]
            stock[key] = stock[key] - 1
    return total
    
    food = shopping_list

so, for me , the point is that food is a list from shopping_list and everything will be fine..thanks eveat and codeninja for the explanation..


#7

Because we are working in the console, it's possible to have variables present with defined values from a previous run. When working through errors after a Submit, do a page refresh to clear memory and start the next run with a clean slate.