Is_prime


#1



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


Its says "Your function fials on is_prime(9). It returns True when it should return False."


It should return False since when it evaluates 9%3 == 0 it's True.


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


#2

however, the first condition tested by loop is: 9 % 2 == 0, which is false, causing else to run, which returns true, which will end the function (in order for the function to end, the loop will break)


#3

Then, what can I do so it continues testing the rest of the options?
I can't remove the else there.


#4

you could place return True outside the loop? This way, the whole loop will run (if its a prime number), and return True

if not a prime number, False will be returned which ends the function (end thus the loop)


#5

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