Stocking Out - contradictory instructions


#1
shopping_list = ["banana", "orange", "apple"]

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 key in food:
        if (stock[key]) > 0:
            total = (prices[key]) + total
            (stock[key]) = (stock[key] - 1)

    return total

print (compute_bill(shopping_list))
print (stock)

The above code does:

1) "add the price of the item to total if the item's stock count is greater than zero"

and then it does:

2) "If the item is in stock and after you add the price to the total, subtract one from the item's stock count"

The code runs and the outputs are correct:

5.5
{'orange': 31, 'pear': 15, 'banana': 5, 'apple': 0}

But code academy says:

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}

This makes no sense because we were explicitly TOLD to decrement the amount in stock which would of course "change" the stock. Subtracting the number of items in stock after a purchase is made is the whole point, isn't it?

What am I missing here?

Thanks so much.


#2

Hello! Hopefully I can help you out.

Let me explain the code in the picture which was successful. I'll start at the top.

Line 21 says: For every item in the list 'food', do the following:
Line 22 says: If that item's quantity in the 'stock' dictionary is greater than one, do the following:
Line 23 says: Add that item's price to the total (0) and save that newfangled value under 'total'.
Line 24 says: Then, subtract one from that item's quantity in the 'stock' dictionary and save that newfangled number in that dictionary (updating the stock).
Line 25 says: Now return the total from line 23.


#3

Thanks for your answer!

I'm wondering though--is it not a problem that your code merely defines a function? It would not actually do anything. For example there would be no output to the console from this code without a function call. I'm perplexed about this.

Thanks again for your help!


#4

Glad I could be of some help :slightly_smiling:

About what's confusing you, yes, I did define the compute_bill function. But so long as there is a list by the name of 'food' with items within it, the function will always return something due to the loop that is part of the function. With the loop, you tell the function that it will consider every item in the list. So the function will always do something when you call it, like how we do when we tell it to look at the food list (by putting 'food' in the parenthesis). Does that help or did I miss your Q? lol. I'm pretty sure that functions must be called for them to do things. But I just started learning Python so there may be functions that work differently. IDK. I hope this helped!


#5

Thanks again--

The code I posted defines the function (effectively) in exactly the same way as yours--so I don't think I'm
misunderstanding how to do that.

But my point was your code does not call the function, does it? If so how?

Maybe the best way to ask this is --are you getting an output to the console?

EDIT

I solved the problem. For whatever reason Code Academy does not want you to actually call the function. So just removing..

print (compute_bill(shopping_list))
print (stock)

..got it approved.

THanks again