I got stuck at "Stocking Out"


#1



Dear All,

Can you please help me as I got stuck at the "Stocking Out" exercise.
My code gives the right result and I followed the "Hint" in the lower left corner according to which I should decrement the stock after one fruit is purchased. I can not get over this excercise because the "Error message" tells me, that I should not decrement the stock.

Can please somebody help me?

Thank you in advance.

This is the link of the excercise: https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/3?curriculum_id=4f89dab3d788890003000096#

I got this error message: "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}"

This is my code:
def compute_bill(food):
    total = 0
    for item in food:
        if stock[item]>0:
            stock[item]-= 1
            total+= prices[item]
    return total


#2

Only one problem,
1. Order of operations

This line,

total += prices[item]

should come before this line,

stock[item] -= 1

making it,

 total += prices[item] 
 stock[item] -= 1

Everything else is fine. :slight_smile:


#3

Thank you for your quick response Bandit. You are completely right.
Although I have just switched the order of the operations but I got the same error message...
Do you have any quesses?


#4

suggestion: remove the function call, the error message indicates you have one, it changes the stock, before the checking script can run, causing problems

@bandit, the order of this operation is irrelevant.


#5

Thank you - it worked after I removed the calling.


#6