12. Stocking out


Hello Community, I am stuck on 12. Stocking out. I get the following 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}"

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

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
price = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3

# Write your code below!
def compute_bill(food):
    total = 0
    for item in food:
        if stock[item] > 0: 
            total += price[item]
            stock[item] -= 1
    return total
n = ["banana", "orange", "pear", "apple"]
print compute_bill(n)


remove this lines:

n = ["banana", "orange", "pear", "apple"]
print compute_bill(n)

the function call changes the quantity, causing problems in the validitaion


Thank you. It worked. Removing those two lines was the answer. For future clarity, what did adding those two lines do?

Did I "confuse" python/ my code by typing in
n = ["banana", "orange", "pear", "apple"]
print compute_bill(n)

What did python try to do with those two unnecessary lines? What made the code incorrect with those additional two lines?

Thank you!


it didn't make the code incorrect, but by doing this, you called the function, which changed the stock, which means the validation can't be done right. (because of how the validation of codecademy works)


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