6. is_prime confusion



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

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

can anyone yell me why the program fails ?
I did some testing and apparently after the break syntax the program doesn't loop back to the for loop in this case. What am i doing wrong ?


break doesn't matter, a function ends the moment a return keyword is reached

Which in your case happens in the first run of the loop. (either if is true, or else will run)

Change the indention of else so the whole for loop can run, you only want to return true after it finishes the for loop


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