13/13 - Add Quantities?


#1

I was curious how to get the loop below to recognize amounts greater than one of each item. The cost of one banana is 4 and one orange is 1.5. When I run the code below I get 5.5 instead of 9.5 ( 2 bananas). It passed me through the lesson, but I would still like to know how to get the quantities recognized. Thanks!

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

x = {"banana": 2, "orange": 1}
print compute_bill(x)

#2

Superfluous, and may be removed.

    total += prices[n] * food[n]

#3

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