Is_prime(error message: Oops, try again. Your function fails on is_prime(3). It returns None when it should return True


#1



Oops, try again. Your function fails on is_prime(3). It returns None when it should return True.


**Can someone help me with this? I've been working on this for a whole day and still don't know why I am getting this error. What I am understanding is that we want to try to divide the number by numbers in the range and if it equals 0 then there is a number divisible and therefore x is not a prime number and we can break the for loop. What I don't understand is why 3 won't work with this and how I can tell Python to go back to for loop and try again if one of (n) numbers returns as "True". **

def is_prime(x):
    if x == 1:
        return False
    elif x == 0:
        return False
    elif x == 2:
        return True
        print "Only even prime number"
    elif x > 3:
        for n in range(2, x):
            if x % n == 0:
                return False
                break
        else:
            return True
            
print is_prime(9)


#2

There is no final return value, so Python sees None as the return.

Do you really need that conditional? 3 fits into the range and will return as True.

There are two instances where you have code after a return. They may as well be comments because they are not parsed. Anything after return is unreachable.


#3

Thanks for your response. I am not understanding. I thought that I returned True in else statement?


#4

But that is not the else that 3 will see. It is the else of the for loop. 3 doesn't see an else.


#5

I have the 3 as the conditional because the range would only have n = 2. I put the else statement outside of the for loop but now the error is saying that it fails on is_prime (5). Here is my code:

d

def is_prime(x):
    if x == 1:
        return False
    elif x == 0:
        print "Not possible"
        return False
    elif x == 2:
        print "Only even prime number"
        return True
    elif x > 3:
        for n in range(2, x-1):
            if x % n == 0:
                return False
                break
    else:
        return True
            
print is_prime(3)

#6

Consider,

  1. Prime numbers are positive integers greater than 1.
  2. Except 2, all primes are odd (not important here).
  3. A deterministic approach does not have very many conditionals.

On the first point, we can rule out everything less than 2 in our first step:

if x < 2: return False

Since we are using brute force (testing all values as divisors) we don't need to pretest for any other values. The range will exclude 2, but all other numbers will be tested at least once. All even numbers will be immediately divisible by 2 so they are ejected in one test.

for n in range(2, x):

If x is 2, the range will be (2, 1) so the loop will not run. A value of 2 will bypass the loop and land at the last return.

return True

Now if we look at the first step, the if has a return, so that means we do not need to have an elif or an else. Just move straight away onto the for. The second step is essentially the second line of the function. Now do the divisibility test in the loop, and return False for any that prove divisible. Again, no else.

So we see that a lot of your code may be stripped away to leave a very slim program in its place.


#7

Thanks for your help. This is my final code and thankfully it was accepted. I added another else like you said

def is_prime(x):
if x == 1:
return False
elif x == 0:
print "Not possible"
return False
elif x == 2:
print "Only even prime number"
return True
elif x >= 3:
for n in range(2, x-1):
if x % n == 0:
return False
break
else:
return True
else:
print "Negative numbers not possible"
return False

print is_prime(-10)


#8

If anything, I said there was no need for elif or else, at all. You have not simplified your code by stripping away unnecessary bits.

if x < 2: return False

That gets rid of the first eight lines. The function should not print anything. It is a utility that returns True or False. No real need for verbosity.

Also as mentioned earlier, break after return is unreachable and does not belong in the code.

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

That and the first line above is all we need in this brute force algorithm.


#9

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