Reformatted code in previous post; code works, but getting an error msg


#1
Here is my code ( i have added several prints for debugging):smile:

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):
    total=0
    for item in food:
        print "item:",item, " price:", prices[item], "stock:", stock[item]
        if stock[item]>0:
            total += prices[item]
            stock[item]=stock[item]-1
    return total
print shopping_list
print stock
n=compute_bill(shopping_list)
print "Total:", n
print stock 


and here is the results I get:

['banana', 'orange', 'apple']
{'orange': 32, 'pear': 15, 'banana': 6, 'apple': 0}
item: banana  price: 4 stock: 6
item: orange  price: 1.5 stock: 32
item: apple  price: 2 stock: 0
Total: 5.5
{'orange': 31, 'pear': 15, 'banana': 5, 'apple': 0}
None

Which to me looks perfect.  BUT I get the following error:

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}

Can anyone help?` Why the error message?

I get the correct answer, but getting an error msg
#2

It is simply that you are calling your compute_bill function.

n=compute_bill(shopping_list)
print "Total:", n

When you hit "Save & Submit Code", the codecademy website looks at your code and try to validate it by looking for specific conditions to find out if you have done the exercise properly.

For this specific exercise, you are asked to define a function but using this function modify your stock (in this case, removes an orange) and since the code validator looks at your stock and find that it is not what it is supposed to be (if you had not used the function), it throws the error message you see.

just comment out the lines and you should be fine :smile:

# n=compute_bill(shopping_list)
# print "Total:", n

#3

Oh, I see. You are saying that they never said to execute the new function and in doing so I ended up with results that did not match their "Code Execute" checker. Thanks for the help.