6. is_prime


#1



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


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


I have no idea what is wrong with my code pls take a look and explain the problem to me


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


#2

if x is 2, it won't run the for loop, so you will need to make a elif to check for 2


#3

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

Is this what you mean? It still gives an error.
Oops, try again. Your function fails on is_prime(3). It returns None when it should return True.


#4

well, your loop won't work for 3 either. So another elif statement or a and.


#5

for x=3 you have range(2, 3-2) so you have range(2,2). That means there is nothing IN range and code doesn't start for loop, so you get None


#7

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