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)