Is_prime


#1


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

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

it blows my mind as at the hint it even says "all numbers less than 2 are not prime numbers!"


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


#2

but 2 is not less then 2? Less then 2 means that 2 itself is not included. 2 is considered a prime number. currently 2 is part of this condition:

elif x <= 2:

which means that false is returned, where true should be returned

Also, realise that a function ends the moment a return keyword is reached, which happens in your case happens in the first run of the loop, you only want to return true after the for loop has finished running


#3

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