Having issues with an is_prime(x) program in Python?
I apologize for asking such a noobish question, but I'm having an issue
writing a very basic program in Python to check whether or not a number is
prime.
Here's my code:
def is_prime(x):
if x < 2:
print ('Please enter a number >= 2.')
else:
if x == 2 or x == 3 or x == 5:
return True
if x == 4:
return False
for num in range (2, int(x/2)):
if x % num == 0:
return False
break
else:
return True
But this returns True for all odd numbers; not just the prime ones. I'm
not understanding why. If someone could point me in the correct direction,
it'd be much appreciated! :)
No comments:
Post a Comment