Stocking Out


#1

Could someone explain why the line:
" total += prices[key]
stock[key] -= 1"
didn’t need to be indented as I don’t fully understand.



def compute_bill(food) :
  total = 0
  for key in food :
    if stock[key] > 0:
      total += prices[key]
      stock[key] -= 1
  return total



#2

Hi @boardwhiz93134 ,

These two lines both need to be part of the if block, and that is why they are indented to the same degree …

      total += prices[key]
      stock[key] -= 1

Whenever the condition stock[key] > 0 evaluates to True, the item referenced by key is available for purchase, therefore we add its price to total, and reduce the number of that item available for future purchase by 1.


#3

Thank you @appylpye
don’t know why I didn’t realise that sooner. :slight_smile:


#4