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

is_prime (9)

return true instead of false .How can I fix it.


a function ends the moment a return keyword is reached, if the return keyword is reached in a loop, the loop breaks


I tried this code on python idle. 4,6 and 8 return false why 9 dont return


the problem is that your loop only makes one iteration (in the first iteration, a return keyword is reached), for 4, 6 and 8 this is no problem:

4 % 2 == 0
6 % 2 == 0
8 % 2 == 0

this conditions are all true.

for nine, the condition is false, so true is returned instead.


thank you very much for your help


You resolved the problem?


This the same issue i'm dealing with right now... how do i get it to iterate through every number and then return false?


by placing return False outside the for loop


yes , my problem was solved