# [is_prime] My code isn't working with 0,1 and 2

#1

I know my code could be written more cleanly and with less code, but this is what I needed up with. And I can't get it to work if x = 0,1,2. It must be and issue with the `numbers = range(2, x)`, but I can't understand why it just don't return `False` and move on to `else:` and enter the loop.

I'm not necessarily looking for a solution, but hopefully an explanation as to what I'm not understanding.

Here is the code:
I have put in some prints and a random number for testing.

``````from random import randint

num = randint(0, 11)

def is_prime(x):
if x < 2:
print x, "is LESS than 2"
return False
else:
numbers = range(2, x)
for n in numbers:
if x % n == 0:
print x, "is NOT a prime number"
return False
else:
print x, "is a prime number"
return True

is_prime(num)``````

#2

Works for me when I put `()` on every print in code.

``````from random import randint

num = randint(0, 11)

def is_prime(x):
if x < 2:
print (x, "is LESS than 2")
return False
else:
numbers = range(2, x)
for n in numbers:
if x % n == 0:
print (x, "is NOT a prime number")
return False
else:
print (x, "is a prime number")
return True

is_prime(num)``````

#3

HA! It sure does! Thanks,
But why? What do the `( )` do? I'm thinking that print is a built-in function, but why isn't it then always requiring `( )`?

#4

Here is a link with an explanation. I don't wanna try to be smart with answers

#5

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