Is_prime indentation bug?


#1

def is_prime(x):
  if x < 2:
    return False
  else:
    for n in range(2,x-1):
    	if x % n == 0:
      	return False
      else:
      	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…


#2

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.


Aside

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

#3

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.


#4

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