Is_prime


#1



What happens with my code, guys? I have already set the conditional statement that if x equal to 2, then the function returns True.In fact, it return None.Why is that?

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

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


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


Replace this line with your code.


#2

but then you should use a or keyword, not and given x can't be 2 and 3


#3

elif x == 2 or x == 3:

#4

right, Our friend reply earlier


#5

last 'else' is aligned with 'for', NOT WITH 'if'. Your last 'else' is aligned with 'if'. Mine is working fine. Good luck!


#6

I 've tried but it still have a problem when it takes 9 as the input!
https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096


#7

yes, @kabirsh answer covers this. A function ends (even if it is in the middle of a loop) when a return keyword is reached, which in your case is in the first run of the loop, you should change the indent of else/return True so it will run after the for loop is finished


#8

Thanks you guys, it work !!