# Is_prime

#1

def is_prime(x):
if x < 2:
return False
elif x== 2:
return True
i =2
while i < x :
if x % i == 0 :
return False
i = i+1
else :
return True
i = i+1

#2

Without formatting its hard to know how your code is structured, But I think your code is this.. as It gave me same error.

``````def is_prime(x):
if x < 2:
return False
elif x== 2:
return True
i = 2
while i < x :
if x % i == 0 :
return False
i = i+1
else :
return True
i =i + i``````

Take 9 as an example and sees why it fails at it.

You see when we input 9, it starts from 2..
and inside the loop when it hits `if`'s tests which says 9 % 2 is not 0 so go to the else part and execute it, so it returns `True` even its not a Prime number.

so How to deal with it?
Erase that `else`-part and just return `True` at same level as of `while`-block?
Because if a number fails all test of "If it is not divisible by any numbers starting from 2 to less than the number itself" then It must be a prime. (as our if-clause does this only).

Other way can be: to use while/else that we learnt in previous section.

#3

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