Problems with Indentation


#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>
Exercise 11: Making A Purchase

I’ve had this problem for a few exercises now where I cannot complete the exercise because I am unable to indent the return properly. If I press enter normally, this happens.

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

I get an error message stating that I got the wrong result because the return is executed before the loop is finished. However, if I press shift+enter, and then indent once, putting the return where it technically should be,

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



I get an “unexpected indentation” error. Please help. This problem is incredibly frustrating, especially when I give up and let the program fill in the code for me, just to realize that the code it generates and the code I originally typed are indistinguishable.


#2

you can insert indent by using tab, and remove indent like you would remove anything else (del or backspace)

ensure return is placed outside the loop


#3

I use tab to indent. My problem is I can’t place the return outside of the loop without pressing shift+enter to go to the next line, and then indenting, but when I do that, I get a “unexpected indentation” error.


#4

but return has to much indent, so you need to remove indent using backspace/del. If the indent is too much (like now) you will have to manually remove it

the editor won’t get it right 100% of the time


#5

Alright, thanks for your help.


#6

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