Is_prime


#1

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

is_prime (9)

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


#2

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


#3

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


#4

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.


#5

thank you very much for your help


#6

You resolved the problem?


#7

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


#8

by placing return False outside the for loop


#9

yes , my problem was solved


#10