def is_prime(x):

    if x>1:
        if x==2:
             return True
            for n in range(2,x):              
                    if x%n==0:
                        return False
                        return True                    
        return False


I dont know what am i doing wrong


a function ends the moment a return keyword is reached (even when return keyword is reached in loop)

so, for x=9 the first case tested by the loop is n=2:

if 9 % 2 == 0

which evaluate to false. so else runs, True is returned, the function ends

What is the condition for a number to be a prime number? x shouldn’t be divislbe by any number in range, this is clearly not happening at the moment

Prime numbers

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