Stocking out: Just need help with explanation


#1



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


#2

What part of your code don't you understand?


#3

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.


#4

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.


#5

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


#6

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


#7

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