Is_prime


#1

For me this code is not working.

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

I am getting an error "
Oops, try again. Your function fails on is_prime(9). It returns True when it should return False."


is_prime-STUCK
is_prime-STUCK
#2

A function ends the moment a return keyword is reached, which in your case happens in the first run of the loop

You might want to change the indent of else/return True so the whole for loop can run.


#3

Now it is working when I removed the indention for the final else statement and made it as for else statement.


#4