6/15 Is_prime returns none


#1



is_prime exercise


The error message given is:

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


Considering I have the raw_input turned into an integer for x and n starting at 2 and ending one number below the input I figure the code should return a True or False instead of none. I have the code break if the function comes back as false. I even have a print statement telling me whether the function returns true or false for each number I use as a raw input. What am I missing here?


 x = int(raw_input("Please enter a number: "))
def is_prime(x):
    for n in range(2, x - 1):
        if x % n == 0:
            return False
            break
        else:
            return True               
print is_prime(x)

Again, printing the results comes back as true or false but for some reason I get the none error. Can anyone guide me towards where the issue is?


#2

the exercise also calls the function with different numbers to see if it works properly. One of those numbers is zero, see what happens when you enter zero in the raw_input.

It returns None, since zero will not be covered by the loop.


#3

Interesting. Thanks for the information. Since the loop fails on 0 any suggestions on adjustment? I can only imagine that since it fails on zero the test comes in during my "if" statement. If that is so is there something wrong with my division test?


#4

the problem is in the for loop:

for n in range(2, x - 1):

of x is zero we have:

for n in range(2, 0 - 1):

the loop will never run, since the loops is trying to take steps of + 1, so it will if this condition:

if 2 < -1

(this is the loop written as condition), is true, it isn't, so the loop will never run

Make special conditions for numbers smaller then 2

also, range does not includes it stop value, so no need to -1


#5

Thank you again for the help! I'm attempting to come up with a condition to address this issue in my code. I'm sure it will come to me soon.


#6

let me know if you need more help, include an updated version of your code

But it would be really good if you come to the solution yourself :slight_smile:


#7

I was able to get my code to work by making it automatically return false if x dropped below 2.

My updated code is below:

x = int(raw_input("Please enter a number: "))
def is_prime(x):
    prime = True
    if x < 2:
        prime = False
    elif x == 2:
        prime = True
    else:    
        for n in range(2, (x - 1)):
            if x % n == 0:
                prime = False
                break
            else:
                prime = True
                
    
    return prime    
print is_prime(x)

EDIT: I was able to do this by looking at someone else's code and making comparisons to my own code and seeing why their's worked and mine did not in addition to your help in pointing out what to look for in my own code.


#8

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