15.6 is_prime understanding IF statement


#1

Hey guys I was wondering if you could help me understand something.

def is_prime(x):
    if x < 2:
        return False
    else:
        for number in range(2,x-1):
            if x % number == 0:
                return False
            else: #this is there the problem seems to be
                return True

This code does not work. It returns errors however

def is_prime(x):
    if x < 2:
        return False
    else:
        for number in range(2,x-1):
            if x % number == 0:
                return False
        return True

This code with the return True outside of an else statement works. I am glad that it works but I am not sure why.


#2

return exits the current function


#3

Thank you very much. This makes everything much easier to understand.