Is_prime


#1



Receiving error message: "Your function fails on is_prime(2). It returns None when it should return True."

Do I need to put in a line for solely 2 stating that it should return True?


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


#2

When I place in this line

if x == 2:
        return True

I receive the error message "Your function fails on is_prime(3). It returns None when it should return True."


#3

As you can see I set a specific condition for 2 and for under 2. You can also break the for loop after the return false for x % n == 0
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 Falshttps://discuss.codecademy.com/t/is-prime/47681e
break
return True


#4

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


#5

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