A day at the supermarket chap 13 - IF/ELSE inside FOR loop

I’m trying to experiment on my own. Below is my code:

stock = {
  "banana": 6,
  "apple": 0,
  "orange": 32,
  "pear": 15
prices = {
  "banana": 4,
  "apple": 2,
  "orange": 1.5,
  "pear": 3

# Write your code below!
def compute_bill(food):
  stockdel = int(raw_input("Enter how many fruits needed: "))
  if stockdel < 0:
    msg = "Please enter number greater than 0"
    return msg
    total = 0
    for ech in food:
      if (stock[ech] > 0 and stock[ech] >= stockdel):
        total = total + (prices[ech] * stockdel)
        stock[ech] = stock[ech] - stockdel
    return total
      msg = "%s not in stock" % stock[ech]
      return msg

fruits = ["banana", 'apple', 'pear', 'orange']
print compute_bill(fruits)
print stock

Problem: Everything works fine until the last ELSE statement is inserted. It keeps giving error on that last ELSE. What I’m trying to do is: ask user to input a number of fruit needed, check input if greater than 0. If it is and if there is enough inventory in ‘stock’ dict, then calculate price (which is happening in the IF condition of FOR loop. If there is not enough inventory, that’s where I’m trying to put an ELSE statement to output a message stating “x fruit is not in stock.” Can we use IF/ELSE inside FOR loop? Or is it multiple IF/ELSE messing it up due to improper order or something? Hope I’m not jumping ahead of the lesson. Thanks in advance.

1 Like