6. is_prime



I can't quite figure out how to account for numbers less than 2 being prime, while also getting 2 to be classified as a prime.I keep getting the error message:Oops, try again. Your function fails on is_prime(2). It returns None when it should return True.

I don't expect that anything will change yet.. I've tried editing this as much as I can think, but I am wondering if there any additional hints that could challenge my thinking on this, so maybe I can come up with something. I know I am missing something, I just don't know what I am missing on this one.

x = int(input())

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



They're not though. And 2 isn't a special case either.

If you read your error message it says you return None (no returned value) which means you didn't hit any of your return statements. There's only one branch in your code which has no return statement, which therefore tells you what happened. You can also use prints to find out what your code does step by step (compare to what you would do manually). Take note of that your loop terminates the function each iteration - so it isn't a loop at all. Also consider which numbers you should try dividing by and see if you're really trying to divide by all of those numbers no more or less.


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