What is wrong with my code

def is_prime(x):

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

if x%n == 0:

return False

elif x < 2:

return False

else:

return True

is_prime(input())

# 6. is prime

**moretanjames**#1

**sercancelik**#2

consider all numbers are prime

before returning True

check if x < 2

if not then check all numbers in range from 2 to x-1

here you need to remember that range function in python includes the first value but not the last one

after all return True

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

Above code worked for me, I think one key is to check x<2 condition outside the for loop. And like sercancelik said, python range() function is start-inclusive but end-exclusive, which means range(2, x) will include every integer from 2 up to x-1. Here we would like to test whether the x is evenly divisble by only 1 and x itself, which is the defination of prime numbers, so the end of range() should be set to x+1, which includes x itself.

**arcpro77152**#5

def is_prime(x):

if x<=1:

return False

elif x == 2:

return True

else:

for n in range(2,x):

if x%n==0:

return False

else:

return True

This one worked for me, but I made it really sketchy with elif... But as long as it worked... all good

The above works well. Here is a slightly different way to do it that also works:

At first I thought Terarockstar had extra code in there by using x + 1, but that is just a clever way to return True instead of using the else in the for loop.

@slythefantastic Using below code is giving me error:

def is_prime(x):

if x < 2:

return False

for n in range(2,x):

if x%n == 0:

return False

else:

return True

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

I figured out that it is due to misplace of else in the code but I couldn't find why it won't work with else as I placed.

**mjcobucci37**#8

This makes the most sense to me, code wise. On a self-deprecating side note, it pains me to know my most common short coming is understanding middle school level math...

**codyroche**#9

I agree Terrarockstar's way is a different solution and it works but I don't like having that check inside of the loop.

Since we know that x/n will = 1 when we reach n=x and our default behavior outside of the loop is to send "True" why have the code in the loop? We can handle that exception outside of the loop and save checking for that state every time the loop runs. We can take it a step further and not bother checking when n=x, as we know that will be true and can be matched by the else statement.

Your example is cleaner and would run faster especially with very large numbers. That said, both were great examples of solutions to the issue.

**slythefantastic**#10

Ha! I agree about middle school math... this learning code thing is a humbling experience.

**slythefantastic**#11

@codyroche I agree. That's why I posted the alternative. It seemed like an unnecessary way to do it. However, I also never thought of it that way, so it was good to see.

**slythefantastic**#12

Sorry for the late reply @ajaxsolver68476:

If you put three backticks (SHIFT + the key to the left of the 1 on most keyboards) before and after your code, then the formatting sticks like so:

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

Alternately, you can use the snipping tool, which is installed on most windows computers nowadays, which looks like this:

In any case, I'm not sure why that code isn't working for you. I've tried it again and it worked well. Maybe post your code using the backticks so we can see spacing/formatting.

**busbyra**#13

Bah, I got stuck on this problem and was racking my brain as to why it wasn't working. When I saw someone else's code looked the same as mind, I got even more frustrated.... until I noticed where my else statement was. Bah! *shakes fist at pre-coffee brain*

**micromaster82861**#14

could you help me understand whats wrong with this code?

def is_prime(x):

```
if x<2 :
print"This number not prime"
return False
elif x== 2 or x==3:
print"This is prime"
return True
else:
for a in range(2,x):
if x % a == 0:
print"The number is not prime"
return False
else:
print"The number is prime"
return True
```

x=input("Enter a number: ")

is_prime(x)

**hypnopilot**#15

What's wrong with mine? Error is: Your function fails on is_prime(3). It returns None when it should return True.

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

**ajaxplayer33854**#19

check your indents

yours is exactly same as mine,

except for the 4,5th line

def is_prime(x):

[tab] if x <2:

[tab][tab] return False

[tab] else:

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

[tab][tab][tab] if x%n==0:

[tab][tab][tab][tab] return False

[tab][tab] else:

[tab][tab][tab] return True

**pssukhwani**#20

@zeziba @slythefantastic

Please check this if it is wrong...it shows me an error...

Thanks in advance

--

Regards

Pradeep

**njb37**#23

I was wondering the exact thing! Thank you for pointing that out or I would've kept going crazy over it.