6.is_prime


#1



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

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

I think the problem is in the last else statement. Is there and indentation problem for the if, else statements?


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


Is_prime what's wrong?
#2

This will return True or False on the first run through the loop, but you should only return True if x % n == 0 is False for all n. You need to move return True outside of the loop.


#3

Aha, I knew it was indentation mistake! (but when I was revising my code before I put the "return True" in the same level as starting "if" statement). This time I returned it in the same level as "for" loop. Thank you :slight_smile:


#4

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