Stocking Out ERROR - console thinks I am computing the bill when I am not


#1



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


Hi:
I am stuck for the stocking out problem. I think the code is right but when I try to run it it gives me the following error:

Oops, try again. calling compute_bill with a list containing 2 apples, 1 pear, 3 oranges and 7 bananas resulted in 35.5 instead of the correct 31.5


I made sure to "comment" the instruction compute_bill after I read in this blog that running it would change the stock before verifying the code! But I still get the error. Even if I erase the compute_bill instruction I get the same error. I tried refreshing the page and nothing works.
I will appreciate any insight and help.
Thanks


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

#compute_bill (shopping_list)


#2

here:

stock[x] -= stock [x] - 1

-= is a shorthand, so without shorthand this would be:

stock[x] = stock[x] - stock [x] - 1

that is a very rapid decrease in stock


#3

Oh what a silly mistake!! Thanks a lot :slight_smile:


#4

Silly mistakes are so easily made. Good you see where you went wrong and understand how to fix it :slight_smile:


#5

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