Stocking out: Just need help with explanation


I got the correct answer through some trial and error guessing, but I'm not sure if this is the best way to learn how to write this type of code.

Replace this line with your code. 
def compute_bill(food):
    for item in food:
        if stock[item] > 0:
            total += prices[item]
    return total


What part of your code don't you understand?


Stock[item] =stock[item] - 1

This was just a guess I wasn't expecting to come back correct. I wanted to know if this was the correct or most effective way write this code. I saw other ways of writing this code and i didn't understand it.


You can also write it by a quicker way, if you want to.

stock[item] -= 1

But yep, I think that CC has nothing bad to say about your code right now.


This means that every time your for loop runs it will subtract one from the selected item's stock,


Your code looks exactly like mine, also after some try and error! Keep it up... :slight_smile:


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