Practice Make Perfect 6. is_prime: function only works until the number 9


Linked exercise

Why does my function stop working on the number 9? In looking at my function, when I go through and test the number 9 in the equations, it seems like it should work out and return False. But I receive this error message: "Oops, try again. Your function fails on is_prime(9). It returns True when it should return False."

The code works with other numbers (both prime and not prime) from 4 to 8. What's happening here?

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

Any help or advice is greatly appreciated.


For some reason, the first line of my code didn't post. it is as follows:

def is_prime(x):


your for loop only runs once. A function ends if a returns keyword is reached. You want to place the return True outside the for loop, so the loop will run until the number is divisible, in which case False gets returned, the function ends. If it comes through all the numbers, it will return True, since the number is a prime


Ah, I understand now. Thank you so much!