is_prime


#1

so this code works for me, but i can't understand why i have to put the 'return true' in that place? it seems not logical.

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

#2

What do you mean that place? At the end?


#3

yes, at the end. and also because it isn't under any statement it is directly under the function


#4

@netninja64558. Let me take a stab at explaining your code.
First, I'd like to say you're code helped me solve this problem, and while explaining it to myself, I think I have an answer for you.

Every function needs a 'return' value. the last 'return' has only one tab, thus corresponding to the final 'return' the function needs to execute.

I treated this as a for-else loop. "For a given parameter, test to see if it's prime by checking for a remainder of 0. If I get a remainder of 0, 'is_prime' should return false. If there is a remainder of non-zero, it's not evenly divisible. i.e. is_prime should return True.

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

#5

if x < 2, it is not prime! (return False)
if x % n == 0, it is not prime! (return False)

If x does not meet the above conditions, it must be prime (return True). If x meets a condition above, it will stop and return False, and your return True will never execute.

Why did you put the return True under an else statement?

else:
    return True

once your for loop is done you want to return true because it must be a prime number if it hasn't met the above conditions

def is_prime(x):
 if x < 2:
     return False
 for n in range(2, (x-1)):
     if x % n == 0:
         return False    
 return True """ for loop is done, we return True because
                x is not < 2 and did not return 0 for x % n """

#6

@kclemens: Just a suggestion, use the

[code][/code]

tag, let's call it as 'tag', so you can actually indent the code and make it more readbale.


#7

ok, i got it. so, in other words - the 'return' is kind of a 'break'?


#8

This is my code

def is_prime(x):
	if x < 2:
		return False
	else:
		for i in range(1, x):
			if x % i == 0 and i != 1:
				return False
				break
		return True

#9

Basicly when the conditions aren't met you need to escape the for loop and return True on the same level the for loop started.


#10

But why wouldn't it always return True? If return True is aligned with the for loop, then wouldn't the return True code always execute within the else statement?


#11

Simple Solution


#12

No because if the function returns False it exits the function, without executing the code that comes after the return statement. This also means that in terrence' code the break will never be reached.


#13

Oh interesting! So if a function returns anything, whether it be a boolean or a string or an integer, it'll break the function right then and there?


#14

Yes, that's how it is.


#15

I have a doubt about your code. Shouldn' t there be an elif or else before the for loop? How is it possible to simply change from an if/else statement to a for loop back to the if/else?


#16

It's a for/else loop. The else branch gets exectued once after the for loop ran.