Is_prime --> keep getting the same error message


#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 been trying for an hour now... no idea why this doesn't work. :frowning: any help?


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


#2

for x to be a prime number, x should shouldn't divisble by n over the entire range

so, we only want to return True after the for loop has finished running, currently your function will return True or False for the first n in range (return keyword will end the function)


#3

Thanks Sir! :smiley:


#4

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