user21398 - 1 year ago 56

Python Question

I recently started coding and encountered something I didn't understand fully while trying to learn Python on Codecademy.

The task was to create a function that would tell if called upon, whether the number was a prime number or wasn't.

So here was my first solution:

`def is_prime(x):`

if x < 2:

return False

elif x == 2:

return True

else:

for n in range(2, x-1):

if x % n == 0:

return False

else:

return True

print is_prime(5)

After running it, it kept giving the message that is_prime(3) was giving False, instead of giving a True.

So after searching a little bit on the Codecademy forums I found that if the last bit of the code was alterd to:

`def is_prime(x):`

if x < 2:

return False

elif x == 2:

return True

else:

for n in range(2, x-1):

if x % n == 0:

return False

return True

print is_prime(5)

it started working normally.

Could anyone explain me how this alteration caused the code to work?

Thanks in advance.

Answer Source

What happens in the first code snippet is that the loop barely gets a chance to run. The `if`

clause inside it has both a then and an `else`

branch, so either one of them will always execute. Because both of them have a `return`

inside it, the function will return as soon as the loop executes for the first time.

Additionally in your specific testcase of `3`

the loop doesn't even run once, because `range()`

specifies an *exlusive* upper bound. `range(2, 2)`

is an empty range. The function reaches its end without a `return`

, and in Python that causes the function to return a special value of `None`

.

The second code snippet was modified to only exit the function if it finds a counterexample that proves that the given number was not prime. In this case it's OK to return early from the function because if one divisor was found, the number cannot possibly be a prime and there's no need to check the rest. Only if the loop finishes without finding one, the `return True`

after the loop will be reached.