fork download
  1. stock = {
  2. "banana": 6,
  3. "apple" : 0,
  4. "orange": 32,
  5. "pear" : 15
  6. }
  7.  
  8. prices = {
  9. "banana": 4,
  10. "apple" : 2,
  11. "orange": 1.5,
  12. "pear" : 3
  13. }
  14.  
  15. # Write your code below!
  16. def compute_bill (food):
  17. total = 0
  18. for item in food:
  19. if stock [item] > 0:
  20. total += prices [item]
  21. stock [item] -= 1
  22. return total
  23.  
  24. pur = ["banana", "orange", "apple", "orange", "banana", "pear", "pear", "orange"]
  25. print "The total price is: {}" .format(compute_bill (pur))
  26. print
  27.  
  28. for w in pur:
  29. if stock [w] > 0:
  30. print "Items bought: {}" .format (w)
  31. else:
  32. print "There was no {} in stock!" .format (w)
  33. print
  34. print "Stock:"
  35. for x in stock:
  36. print "{} --> {}" .format (x,stock[x])
Success #stdin #stdout 0.01s 9016KB
stdin
Standard input is empty
stdout
The total price is: 18.5

Items bought: banana
Items bought: orange
There was no apple in stock!
Items bought: orange
Items bought: banana
Items bought: pear
Items bought: pear
Items bought: orange

Stock:
orange --> 29
pear --> 13
banana --> 4
apple --> 0