Practice Make Perfect: Is_prime


#1

Hi everyone,
I am having trouble with this exercise. So, after looking at other discussions and not getting an answer, I took to you all. My code is as follows.

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

So you know the for is single indent, the if is double, the return false is triple, the else is double, and the return true is triple.

This is my error message.

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

I hope to hear back soon. Thank you.


#2

The error message says, it is not getting the expected answer when it tries to find out if 0 is a prime number.

I see you are checking if the number is in range 2 through x-1.

so when x=0 you never even test it you just return it as true because 2 through -1 does not happen because well 2 >-1 so it already went through everything it could.

the error message says the function it is having an issue running so for this its running is_prime(0) to check if your code works and some other things.

.
so you might want to look into adding an if statement somewhere to see if the number is less than 2.

also in case you did not know: anything below 2 is not a prime number


#3

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