Is_prime


#1

Hi there,

I'm a little confused as to why the first return(True) on line 6 of my code below works...

Why don't I need an else statement prefixing the return(True) statement? Does python automatically assume an else statement in code structures like this?

Any and all feedback will be greatly appreciated.

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


#2

Better question, why not? If we want to return True after the loop has finished running, this one way of achieving this (we could also use for/else)

else doesn't has to be used if we want to return True


#3

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