Is_prime - Error "Doesn't work for -10"


#1



Practice Makes Perfect - is_prime


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


I already solved it by writing
if x<2:
return False

However, I wonder why I get this error message specifically for is_prime(-10)?
Thanks for your help!

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


#2

negative numbers can't be prime numbers, that is why you got an error messge.

If you don't write a condition for it (i read you did), return True on the last line would be reached.

The exercise test a bunch of numbers (including minus ten) to see if your function works correctly


#3

Add an if statement to your function that returns false for negative numbers like so:

if x < 0:
return False

That should do the trick.


#4

he already did that? He had a question about why he got this error message


#7

yes, but that was the point, he got it to work, but he had a specific question about the error shown


#8

Thanks for the answer! I was mostly wondering why it specifically said '-10' since it is obviously wrong for every number below 2.


#9

codecademy has a number of test cases, -10 is the first number it use to check if you function works correctly for negative numbers. Why -10? Only the person who created this course can answer that question


#11

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