Stocking out problem


#1



https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/2/3?curriculum_id=4f89dab3d788890003000096

The condition of the task is to define a compute_bill function, which takes a list of groceries, checks if they are available and sums up their price. We are the understandably told if the item is purchased to subtract one from stock counter. However, when I define such a function, the error message I get tells me I shouldn't change the stock numbers (even though that is one of the conditions of the task):

""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} ". Does anyone have any idea why, because I think the function itself does what it is supposed to?


        

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 x in food:                
        if stock[x] > 0:
            x = prices[x]       
            stock[x] = x - 1
            total += x            
            print total           
        else:
            print 'out of stock'
    return total                  
        
compute_bill(['banana','apple','orange','pear'])


#2

remove the function call:

compute_bill(['banana','apple','orange','pear'])

it changes the stock before the validation can run (this in the error message)


#3

In the end I just clicked save and upload code once again and it worked, without changing the code...Very confusing. Going to try that now though. Thank you.


#4

3 posts were split to a new topic: Stocking out


#5