Is_prime(0) error


#1

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

error: Your function fails on is_prime(0). It returns None when it should return False.

the "zero" input is handled in the first "if"....


#2

Why it fails at x = 0 i.e is_prime(0)..
lets check behaviour of program at 0..

at 0 when it seeks for for loop's range..
range will be like range(2,0-1) (asx = 0) which is range(2,-1) an empty list and your function returns None.

hints**.
Create a condition to check if value is 2 ,if its 2 return True.
You also need to check value(x) less than or equal to 1 -- return False.
You also need to fix your range.

another hint..
use for/else


#3

You have the correct logic just syntax placement/control flow for one. We know a prime number is anything less than two so you can go ahead and put that check outside your loop,

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

Then inside your loop you only need to loop from 2 to the number no need for the -1,

for n in range (2,x):


Your only check should be to check if the numbe ris divisible by itself and equal to 0 if it is you return False then outside your loop you return True for the latter. All in all your loop should be something along the lines of,

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

#4

Interesting the fact "range(2,-1)" broke it without interpreter's error....
i am curious now: does it try to make loop from "n" to -1 starting from 2 (2,1,0,-1) or what?

This finally works:

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

meh! the -1 was there since the instructions explicitly ask for that, thought the script check code was going to look about that. It's time to rest now seems my brain's not that awake atm.... and sorry for my english!

THX


#5

Aside

When an if statement returns, we don't need the else.

if ...:
    return ...
for ...:

return True

#6

Hey, I will post on this thread as I also had problems with this exercise. I hope that's okay.

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

This code worked, and I'm sorry if I sound very stupid (I'm learning :[ )
My question is, why do we need this code here

if x <2:
        return False

Is it not enough to have the for i in range (2,x): ? as we start the range from 2 ? Help me understand this. I'm confused.


#7

As an aside, first rule of programming is do not use code that you do not understand. That is why it is so important we tackle problems on our own steam. We encounter each smaller problem face on, rather than trying to explain away someone else's solution.

That aside, we need that line of code to capture all numbers less than two. 1 is not prime, neither is 0, and prime numbers are not negative.


#8

Adding on to w4trl00, why doesn't it break at is_prime(2)? 2 / 2 yields no remainder, so it should return False, but 2 is the only prime number that is divisible by 2.
Thanks.


#9

But two doesn't get divided since the range is (2, 1). When x is 2, the loop is bypassed and flow goes to the last return... True.


#10

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