My 'is_prime(x)' doesn't work for x==9


#1



Now I'm stuck for Practice Makes Perfect - is_prime.


Maybe I think that my code doesn't work correctly. Because the code stops after 'return'.
So I thought that I need to change my code but I don't know how to change this.
And here's my code below.

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

#2

a function ends the moment a return keyword is reached, which happens in the first run of the loop, remove else return True

now true is only returned after the loop is finished running (which indicate the number is a prime number)


#3

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