> Is_Prime - help


#1



Is_Prime


I've made different codes and always get "Oops, try again. Your function fails on is_prime(3). It returns None when it should return True."

I've seen other solutions here but using some elements I don't understand.


def is_prime(x):
    if x < 2:
        return False
    elif x == 2:
        return True
    else:
        for n in range(2, x-1):
            print x
            print x / n
            temp = x / n
            if type(temp) == int:
                return False
            else:
                return True

I also tried a different code like this but once more, no results.

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

But in this case I get another error: "Oops, try again. Your function fails on is_prime(9). It returns True when it should return False."

I'm stuck here...


#2

The first one, like python says, won't encounter a return statement for x=3 since it'll reach the end of the function.

The second one has a for-statement, but it isn't actually a loop because it always exits the function each iteration. The first one has the same problem.


#3

Thank you ionatan,

I see what you say on the first one, but if I change the range to (2, n), this way the number three is tested, but since it will result and integer when divided by itself, the result became False and it should be True.

I think the second way is better to test, but I don't understand why it returns True for number nine.

If I understand what you are saying, in this second method the function ends each time, but since is has for isn't he last result that counts?

I'm totally lost on this one


#4

Hi again,

I found it after burning some more neurons...

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

Just by removing the last return True form inside the loop.

Thanks for the help.


#5

range(2, n) does not include n, otherwise range(2, 2) would result in [2] which would cause that function to encounter a return statement and therefore not return None

You say that onle the last return should count, well, what should be done with all the previous ones? Re-think what return does and try to only rely on behaviour that you have been promised in some way that it exists. Wanting something to behave one way doesn't make it so. Additionally, you can fairly easily follow along in what's being done by adding print statements where you write which action is currently being carried out.


#6

I think you need add an "break" after the "if x%n==0: return False".or it will go on the loop.


#7

Read up on what return does

Also, you can test whether that is the case


#8

I definitely need to study well this for lops.

I did it by removing the last return from inside the for loop

Now it is ok

Thank you


#9

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