12. Stocking Out


#1

shopping_list = ["apple", "orange", "pear", "banana"]

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):
    total = 0
    
    for item in shopping_list:
        if stock[item] > 0:
            total += prices[item]
            stock[item] -=1
        
       
    print total 
    print stock
    return total

compute_bill(shopping_list)

Code output is correct as I see, but there is a weird warning which keeps coming up.
Not sure if this is a codecademy interpreter constraint.

Any assistance appreciated folks.


#2

when you call the function:

compute_bill(shopping_list)

you can pass shopping_list as argument, which is then copied into the function parameter (food), which means in the function you can loop over food:

for item in food:

this will mean you can call your function for different (shopping) lists instead of one fixed list.

then one more catch, remove the function call, it influences the stock before the code can validate.


#3

Ok, the first point makes sense to actually make use of the parameter and make it use any value passed in the code.

But, the second point, which is remove the function call - well, how do I check the output of the code then if I dont call the function. Dont quite understand.


#4

the problem is the way the validation is done, calling the function will change the stock, causing problems in the checking script, in order to pass the exercise you can't check the output. If you run this code anywhere else calling the function is fine, for passing this exercise it is not, so you can't check the output, you just have to trust your code is right, otherwise the lesson will call the function for you and throw a error message


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.