#### Gamerboy

##### Distinguished
So I'm going through the codecademy python course and one assignment is to create a function that can determine if a number is prime or not. I don't understand what the difference is between these two examples. The first one doesn't work but the second one does. I get this error message when I try to submit the first one:

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

I got past the lesson but I would like to have an understanding of why these two sections of code are different. Why doesn't it recognize the return after the final else in the first example? Thank you

#This one doesn't work

Code:
``````def 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
else:
return True``````
#This one does

Code:
``````def 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``````

#### rgd1101

##### Don't
Moderator

yes, that how "return" works.

#### dudeman509

##### Estimable
Bug I guess. Either way would accomplish the desired result. If followed by else statements are generally good practice.

#### rgd1101

##### Don't
Moderator

need to put in a code block. also read the line #this else is not in the one that works

#### Gamerboy

##### Distinguished
How do I put it in code blocks? Thanks

#### dudeman509

##### Estimable

When you reply to an answer using the link in the post, it'll bring up an advanced editing window. The code window is just after the 'A' icon.

Thanks

#### rgd1101

##### Don't
Moderator
put 3 in the first one, can you tell me what would this do when x is 3?
for n in range(2, x-1):

#### Gamerboy

##### Distinguished
If I understand it correctly it would see if 3 / 2 has a remainder of 0 which of course would return False.

#### rgd1101

##### Don't
Moderator

that is after that. I mean right on the line.
http/pythoncentral.io/pythons-range-function-explained/
or
https/docs.python.org/2/library/functions.html#range
"If step is positive, the last element is the largest start + i * step less than stop;"

#### Gamerboy

##### Distinguished
Ah that's right. range() stop doesn't include the number it stops at so it wouldn't include 2. So when x = 3 it doesn't get checked. Nice

#### Gamerboy

##### Distinguished
Now I get another error when I get rid of the x-1.

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

C++:
``````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``````

#### rgd1101

##### Don't
Moderator
ok, use a debugger or if you don't have one, use a piece of page
Write down what the
for n in range(2,x): and it code would do.

#### Gamerboy

##### Distinguished
I understand why the second section in the OP works now. x = 3 bypasses the for loop entirely the way it was written.

That should check if 9 % 3 == 0 which would be true so it should return False. Am I missing something else?

Moderator

#### Gamerboy

##### Distinguished
it should run through and check:

x = 9
9 % 2
9 % 3 and at that point it would be true so it wouldn't need to check 4 - 8 right?

#### Gamerboy

##### Distinguished
and can I deselect that answer as the best one? He apparently missed the same thing I did originally.

#### Gamerboy

##### Distinguished
or is it just checking 9 % 2 and then moving on to the else statement when that evaluates to false?

#### rgd1101

##### Don't
Moderator

yes, that how "return" works.

#### Gamerboy

##### Distinguished
I got it. It makes sense now but my head was about to explode haha.

the else inside the for loop is executing the first time the if is false. The else should be associated with the for instead of the if.

Thanks a lot rgd1101

#### rgd1101

##### Don't
Moderator

You welcome, have fun.

Apps General Discussion 5
Apps General Discussion 5
Apps General Discussion 4
Apps General Discussion 4
Apps General Discussion 1
Apps General Discussion 1
Apps General Discussion 1
Apps General Discussion 1
Apps General Discussion 1
G Apps General Discussion 5
Apps General Discussion 11
Apps General Discussion 6
Apps General Discussion 1
Apps General Discussion 2
Apps General Discussion 3
Apps General Discussion 4
Apps General Discussion 1
Apps General Discussion 6
Apps General Discussion 3
Apps General Discussion 3