is_prime question


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096
<In what way does your code behave incorrectly? Include ALL error messages.>
I am trying to make my own code, which I believe is important to the learning process, rather than come to the QnA whenever I get stuck. Alas, I needed to get some help on this one.
<What do you expect to happen instead?>
No matter the number I put in, it prints False unless I input 0 which then returns True. I was hoping someone could explain why this is exactly.

My code: ```python

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

print is_prime(3)


This is a similar code that I found that works:

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

I just wanted to know what the exact difference is. 
<do not remove the three backticks above>

#2

There are several problems with your code, but I’ll just help with the first one. You’ll find the rest, I’m confident of that :smiley:.

So you’re saying there’s a problem at x = 0. Let’s simulate the program flow:

for n in range(0,0):

This will not go trough the loop at all, since there are no numbers in this range (the last number is exclusive).
If you don’t go trough a loop, it will automatically go to the else clause of the loop, if you have it:

else:
        return True

When it goes into the else clause, it returns True which is not what you want. Do you know how to fix it (hint: please don’t just change it to return False :smiley: )


#3

These codes are very different. Lets start with something important: The moment a return keyword is reached, the function ends

Now, lets add a print statement to your code and pick a prime number (for example 11):

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

print is_prime(11)

i added a print n, so we can see where the loop ends. It prints 0. So, something gets returned, the function ends, which includes the loop, let see which condition is true by adding another print statement:

if n%2==0:
   print "i get here"
   return False

oops, it seem that n%2==0 is true. This is a problem, and this is where the other program is very different, lets take a look at two lines of the other program:

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

here, the loop already starts at two. But the crucial difference is that he divides the prime number with the numbers 2 till 10, to check if the remainder is zero (indicating the number is not a prime) Your code loop from 2 till 10, dividing it by two all the time. In this range, there are many values which are dividable by two


#4

That makes a lot of sense. I forgot that (0,x) x is the number the ending and is also excluded. It kind of explains the rest the problems in my code as well. Thanks for the help!

Both explanations helped a lot. Thanks guys!


#5

As I said, that’s not the only problem. Take a look at @stetim94 's awesome answer as well.


#6

If you have any further question, do ask them. Being able to solve this problem would greatly improve your understanding of coding :slight_smile: Also, if you have any further question, post an updated version of your code, so we can help you further if you want