Is_prime(x) # struggling with x = 2


#1



Ok I get the following error message "Oops, try again. Your function fails on is_prime(2). It returns False when it should return True." again and again, although going through my code I don't really see how could my function return "False" for x = 2.


def is_prime(x):
    if x < 2:
        return False
    else:
        for i in (2, x-1):
            if x % i == 0 and i != x:
                return False
                break
            else:
                vcdt = True
        return vcdt

print is_prime(2)

I thought I had shortcutted that error including "i != x" as a condition on line 6 so when x = 2 the code should just ignore that if and get directly to the else and thus return "True". I know I could just add a line like "if x == 2 \ return True" but I'm trying to get a more generic code.
Do any of you see the problem?
Thanks a lot and cheers!

P.S.: is that break on line 8 necessary to break not just the loop but also the function, or does the function break by itself when returning something?


#2

You've already ruled that out earlier in the code, that will always have the same result

How many iterations is that when x is 2?

It never gets executed, it is not reachable

There are cases where vcdt hasn't been defined by the time this is reached


#3

Hi ionatan,
thanks for the answer. I don't agree nor get some of the points you've made:

Where do I specify that i and x have to be different before that? As I see it, I need that line to avoid the function running into this particular if when x is set to be 2.

Right. I guess in this case the loop would run twice: first setting i = 2 and finally setting i = 1. The latest is the one messing up my purpose.

Why shouldn't it be reachable? Because the return does automatically break? Otherwise I don't see the reason for it to be so.

That's why I added the break at line 8, to make sure line 11 (return vcdt) is only reached if it has a value. Otherwise it is just bypassed.


#4

I read that as

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

So yes, your loop does two iterations with i=2 and i=x-1

That's probably not what you want to be doing though.

For example, is 99 prime?
Not divisible by 2, and not divisible by 98 .. so it's prime then? You would need to try dividing by more numbers than that, because 99 is divisible by 11, 9, and 3


You can't do anything after having exited the function, because the function is no longer executing, it was exited.


#5

Since you've already found out that you're iterating through the wrong divisors, you've narrowed it down and can now reconsider how to compute the divisors that you actually want. The aforementioned range function is appropriate for that (instead of always iterating through exactly two numbers)

Iterating from 2 up to but not including 2 is a total of zero iterations, meaning vcdt would not get assigned to because that's done in the loop. besides, you only ever assign True to that variable, so is it necessary at all?


#6

Wow huge mistake. I forgot the in range().

Still there's a couple of things I don't get:

  • returning something should break the function. Hence, you just get to that return vcdt on line 11 when running all the conditions above have been bypassed. So the variable should have a value if at all called. Isn't it so?

  • I rewrote my code because I still got error messages:
    How is this:

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

which runs perfect and smoothly, different to this other one:

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

which does simply not work?
Thank you very much for your support!


#7

If you loop 10 times, and each time you exit the function, then you will do a total of one loop. Not 10.
Read up on what the return statement does. It doesn't do what break does, because then one of them wouldn't exist, no point having two statements do exactly the same thing. Besides, what would returning 10 times do anyway? Doesn't make sense to return after having returned, because you're no longer where you were: Lisa returns back home. Then, Lisa returns back home. <- can't, already home, doesn't make sense


#8

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