# 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
}

def compute_bill(food):
stockdel = int(raw_input("Enter how many fruits needed: "))
if stockdel < 0:
msg = "Please enter number greater than 0"
#compute_bill(food)
return msg
else:
total = 0
for ech in food:
if (stock[ech] > 0 and stock[ech] >= stockdel):
total = total + (prices[ech] * stockdel)
stock[ech] = stock[ech] - stockdel
else:
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.

2 Likes

Hello there, krisiyah ! I found your mistakes:

1. Identation

Remember:

1. Rightly ident if/else statements.

``````stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}

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

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

Correct code:

``````stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}

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

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

Be really attentive to identation)))

Python is one of the most and powerful programming languages

I will be really glad if I helped you))) If I really smash dat solution button))) If not, message me or reply here)))

Stay wild, code strong | /jus_iljas

I just want to say your point 2 is wrong. name == “main” is to create an area for code that will not run if the file is imported, it will only run if the file is being run directly.

To say the code is wrong is incorrect, it is perfectly valid without using the name == “main”, it just means that will cause stock and compute_bill(fruits) to be printed. So it won’t even have any breaking side effects to not use it and import. It is good practice however. Also if you want to go down that route then your solution is wrong because I would expect a main function that gets called rather than chucking all the logic into the name == “main” code block. Basically, please don’t confuse people by saying their code is wrong because it doesn’t follow convention or best practice when it is perfectly valid, especially if there is no intent to import the file.

Also, with regards to point 3, this is clearly python 2 not 3, thus print is a statement not a function, so you are passing misleading information here too.

2 Likes

jagking, hi there. The question is old, so I decided to answer with the information that I know. Anyway, thank you for correcting me. I’m learning Python too))) I made a note of your information. So, as I understand the only mistake he did is identation?

I edited my post))) Thank you twice)))

1 Like