6. is_prime


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096


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

is_prime(x)


#2

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.


#3

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