# Is prime - return indent

#1

Hi! why does this code fail:

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

while this one succeeds?:

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

the difference is that the ‘else’ statement of the code that succeeds has the same indentation as the ‘for’, while the failed one has the same indentation as the ‘if’. Thanks in advance!

FAQ: Learn Python - Practice Makes Perfect - factorial FAQ: Learn Python - Practice Makes Perfect - is_prime
#2

By default, a function returns None at the end of the function. If we want to return something else at the end of the function, we can use the return keyword.

Given return is the last thing a function does, when a return keyword is reached, the function ends

so when using `if/else`, the loop ends in the first iteration, which is desired behavior to see if `x` is a prime number.

using `for/else`, `else` only runs when when loop condition becomes false, this can’t happen when the loop breaks/returns. This approach make sense, given `x` is only a prime number when it gets through the loop successfully.

#3

Hi thank you! But could you explain again why:

for n in range(2,x):
\if x % n == 0:
\\return False
\else:
\\return True

*This code returns True for is_prime(9)

However,

for n in range(2,x):
\if x % n == 0:
\\return False
else:
\return True

this code returns True for is_prime(9)

How does the indentation affect the result?

#4

The whole point of my reply was explaining precisely that? Your reply doesn’t even indicate you understood anything i attempted to explain.

11 is a prime number, because 11 is not divisible by 2, 3, 4, …, 10

9 isn’t a prime number, because 9 is divisible by 3.

However, if you use if/else:

``````   for n in range(2, x):
print n
if x % n == 0:
return False
else:
return True

// function call
print is_prime(9)
``````

you can see that the loop only makes one iteration (because of the print statement i inserted). the if condition test `9 % 2 == 0`, which is false, so the else clause runs (given you use if/else), which returns True, which will cause the function to end, as i explained, a function ends when a return keyword is reached

the indent determines when `return True` is reached, which has massive impact on the way your program works, if your program works correctly (for/else) or faulty (if/else)

#5

Oh my god thank you so much! So can i just confirm, when using the if/else, the reason why the result was True was because the moment it hit the first part of the range, 9 % 2 == 0 which is False, it immediately moves on to the else statement, hence returning True. In other words, for the statement to end on the “if” of the if/else, every part of the range must return True.

However for the for/else, the moment it returns false on the loop, the else statement doesn’t run, because the loop condition for the ‘for’ is false?

#6

that is kind of the point of `if/else`? `if` some condition is met, do something `else` do something different/else.

no, `return` will end the function immediately. Code after a `return` keyword will never run:

``````def example():
return True
print False # print won't be reached, given return ends the function

example()
``````

to truly understand `for/else`, is to explain `break` as well:

``````for i in range(5):
break
else:
print True

``````

the loop breaks, so nothing happens. The else clause won’t run given the loop broke.

``````for i in range(5):
print i
else:
print True
``````

now the loop makes all iterations without breaking, so the loop condition becomes false, so the else clause will run this time.

Its more difficult to see and understand this concept in python, in many languages a for loop would look something like this:

``````for (var i = 0; i < 5; i++){
console.log(i);
}
``````

this is a JS example, we much clearer see the loop condition (`i < 5`) here.

#7

Thanks man! You are amazing! Thanks for taking the time to explain!!!

#8

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