Is_prime - None instead of False


#1

Code looks like this :

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

And it returns :

“Your function fails on is_prime(0). It returns None when it should return False.”

I gave myself pretty much time to figure out something but I still just cant understand why its returning None.


#2

When x is 2 (or less, in this case), it skips the loop. There is no return statement at that block level so there is no return value, hence, None. Cut back the indentation on your return True line.

Recall that numbers less than 2 cannot be Primes. You need a case for that return branch.


#3

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