What's wrong with this code


#1



Practice Makes Perfect


function fails on is_prime(3). It returns False when it should return True.


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


#2

Hi!

Let's take a look at your code line by line.

def is_prime(x):
    if x < 2:
        return False

Here we establish the condition that all numbers under 2 are not prime. That's taken care of!

    for i in reversed(range(1,x)):

I see where you are going with this... although it's perfectly fine to loop from the number backwards to 1, there's a small problem with your range.

  • Remember, with range(), the start and stop parameters are a little strange:
    • start contains the first number you want your range to include
    • stop, however, contains the last number plus 1 that you want your range to include

So for example, if we want a list containing [2, 3, 4, 5, 6, 7, 8] then our range function would look like: range(2, 9)

Additionally, we don't need to include 1 since we've already set a condition for numbers below 2 to return False.

So: let's correct the range function. So far we have:

def is_prime(x):
    if x < 2:
        return False
    for i in reversed(range(2,x-1)):

Next block in your code:

        if x % i != 0 and i != 1:
            return False

Remember, we're trying to solve for each number passed to our function if the condition is_prime is True or False. So a number that can be evenly divided by another number other than 1 or itself(in other words, if the result of x % (1 < num < x) equals zero) should return False because it is not prime.

In your code, you return False if a number cannot be evenly divided by another number, which is the opposite condition. Let's change != to == to correctly test for numbers that are not prime.

Also, we only need one condition here because again, we have already handled the condition that x is 1 or 0.

So far:

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

Let's think about this for loop for a second. If we loop through all of the numbers in range(2, x-1), and never return False, what does that tell us?

  • Remember, if a function reaches a return statement, the function exits without any further computation

This means our program never found a number that evenly divides into x and therefore, we will exit the loop without ever returning False, since our if x % i == 0 condition was never satisfied.

If we know our x cannot be evenly divided by any number greater than one and less than itself, we can say it is prime, so is_prime should evaluate to True. This means we can add our True return statement just after the loop.

All together:

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

Hope this helps your understanding! :slight_smile:


#3

Hi, thanks alot wowpalvabbit, i didn't know about the range and always thought it was one number behind the stop. I am grateful for your help concerning the if statement and putting the return true at the end.

Again thank you very much.


#4

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