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(-7). It returns True when it should return False.


The program should take -7 as prime as a correct answer not the opposite


def is_prime(x):
    count = 0
    n = 2
    if(x > 2):
        while(n < x):
            if(x % n == 0):
                count = count + 1
            n = n + 1
        if(count > 0):
            return False
        else:
            return True
    elif(x >= 0 and x < 2):
        return False
    elif(x == 2):
        return True
    else:
        x = -x
        while(n < x):
            if(x % n == 0):
                count = count + 1
            n = n + 1
        if(count > 0):
            return False
        else:
            return True


#2

this line:

elif(x >= 0 and x < 2):

remove the x >= 0, given all negative numbers are not prime, just x < 2 will do. This way, you covered all negative numbers.


#3

thanks it worked great but if you can explain why did I have to do this while I have already made a part for when x is negative


#4

where is this part then? important: Negative numbers can't be prime numbers(!!)


#6

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