Is_prime: Why is it not working with 0, when i covered that value?


#1



Link to exercise:
https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096#

When i run the code, i get the following error message:
Oops, try again. Your function fails on is_prime(0). It returns None when it should return False.
That is correct, but i don't understand why because i covered that with my first 'elif' statement.
So, i expected the code to return 'False' when x was equal to 1 or 0.

Can anybody please explain to me what is wrong with my code?


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


Is_prime: I don't know what I am doing wrong
#2

because the loop won't run for x=0, the loop condition evaluate to false the first time, so the loop never runs, so elif is never reached


#3

How can i change that?


#4

I wouldn't say the loop condition is false - this isn't a while loop - rather range(2,-1) outputs an empty list (because by default the step in range is +1, so anything less than or equal to the starting number will output an empty list). So there is nothing to iterate over and so n never gets assigned a value. This can be shown like so:

>>> for n in range(2,0):
	   pass
    print n

Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    print n
NameError: name 'n' is not defined
>>> for n in range(2,0,-1):
            pass
    print n
1

In the first instance, we get a NameError saying that n is not defined because there was nothing to iterate over and so nohing to define n with.

This explains why None is returned too. Due to nothing under the for statement being run, the function will run to the end without hitting one of your return statements. But functions will always return something and by default this is, you guessed it, None.

What you need to do is make sure that if x is 2 or less it won't throw the rest of your code off.

That said, your code won't work as you intend. It will just define whether x is even (False) or odd(True). The reason being once a return hit the function stops running and sends back the return. So in the case of x being an even number, the first thing n will be is 2, every even number can be divided by 2 with 0 as the remainder and so it will be True because of this line:

        if x % n == 0:
            return False

And the opposite is true for x being an odd number - the first number n will be is 2 and no odd number can be divided by 2 with 0 as the remainder and so once it hits the last elif statement it will return True because of this line:

        elif x % n != 0:
            return True

#5

So, how can i return True for real prime numbers and False for not prime numbers, with the loop still running for the range from 2 to n. And how can i tell Python to return False for the values 1 and 0?


#6

You can simply place the an if condition before the loop to cover numbers which would produces an empty list

Well, for a number to be a prime number ,the whole loop needs to run succesfully, so we only want to return True after the loop has finished running


#7

So, like this?:

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

#8

But what to put inside the for loop?


#9

inside the loop you need to determine if the number is not a prime number, you must see if xis divisble by any number in range n

2 is also a prime number

a function ends the moment a return keyword is reaches, so be careful how to indent return True, as you currently indented your elif and else are not reached


#10

So:

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

#11

to check if x is divisble by n the remainder must equal zero, now we have determined the number is not a prime number, so we must return False

this line:

return True

is not correctly indented. Your elif and else are never reached, given return will end the function, so for x=-5 true will now be returned


#12

Like this?:

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

#13

Is something wrong with my explanations?


#14

I don't think so, but i'm only 13 years old and very new to Python ok, and i can't figure out how to do


#15

Started learning Python yesterday or something like that


#16

We want to return False when x is divisble by n, so return False should be in the if clause:

if x % n == 0:
     return False

then after the loop (but still inside the if x >= 2 clause, we want to return True, because if the whole loop finishes running, we want to return True, given we have succesfully determined the number is a prime number


#18

Yes, that looks good :slight_smile: Does it work?


#19

Yes it works! Thank you so much for the help :grinning:, i was totally confused


#20

#21

You're welcome :slight_smile: