6. is_prime exercise returns none error


#1

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

My code returns the following error:

Oops, try again. Your function fails on is_prime(2). It returns None when it should return True.

I expect it to work. I cannot see why it doesn't work. I have consulted other similar topics, haven't found the answer there. A nudge in the right direction would be great. Otherwise, I fear I could stare at it forever and not figure it out.

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

#2

A function ends the moment a return keyword is reached, if this happens in a loop, the loop simply breaks. When should you return True? And does your code currently does this?


#3

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