3

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.

4
  • 1
    Think about when your first version returns true. Work it through an example with paper and a pencil. It should be pretty obvious then what the error is (although you are by no means the first person to make this mistake when calculating primes, even just going on Python SO questions today, so don't worry). Commented Apr 24, 2016 at 22:01
  • 1
    I don't see how is_prime(3) would return either True or False for the first version, as range(2, 3-1) is empty and thus the code never gets a chance to return anything other than the default None. Commented Apr 24, 2016 at 22:02
  • 1
    @TigerhawkT3 I guess the grader saw None as false-y Commented Apr 24, 2016 at 22:03
  • @jonrsharpe - Yeah, one of the reasons I don't like those automatic graders. =\ Commented Apr 24, 2016 at 22:05

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.