Is_prime indentation bug?


def is_prime(x):
  if x < 2:
    return False
    for n in range(2,x-1):
    	if x % n == 0:
      	return False
      	return True

It keeps saying this error:

  File "python", line 7
    return False
IndentationError: expected an indented block

I’m kind of at a loss…I’m indented and reindented many times and keep getting this same error. Does anybody see anything wrong with the code that I don’t? I feel like the syntax is correct as far as I can tell…


Yes, the return False should be indented further (remove the whole thing and type in four space increments to be sure).

Should not be inside the if or the loop.


For simplicity, when using return in an if clause, we don’t need an else clause.

if x < 2: return False
for n in range(2, x):
    if ... : return False
return True


Thank you! That’s good to know that the True choice can be just returned like that instead of under an else:

I like codeacademy but these tutorials seem to be very finnicky in terms of their spacings.


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