A day at the supermarket: i dont understand this code


#1

i did passed this section.but i dont understand the result of this code.


#2

the function is never called, so nothing gets printed, so the codecademy console show None (none printed)


#3

yes i know about that. i know what the output is. but the coding from the instructions is what i dont understand @stetim94


#4

what is it you don't understand? everything is to broad to answer


#6

when stock was greater than zero. the if statement instruct stock block is what i dont get clearly. i tried coining out few examples on my own but i still got lost


#7

First you are checking if the stock value for a specific object is greater than 0. (You can't add nothing into your cart and get charged for it lol)
if stock[compute] > 0

Then you are adding the price of the item multiplied by how many items you are purchasing.
for compute in food #This loops price addition by how many objects in stock
...
total += prices[compute] #adds the price of a single stock to total but get looped during iteration until 0

Then you are subtracting one from stock since it was purchased and no longer on your stocks.
stock[compute] -=1

Finally it takes the total price and returns the value to where ever the function was called from.
return total


#8

BINGO!! thanks there. the iteration makes it more explainable. my conflict was the increment of price forgetting the fact that stock deals with price in our total.