Python newbie here, so bear with me...
Unfortunately there's no "support" for these tutorials, except posting questions in a Q&A forum and maybe another student can help. I know that there are a ton of Python prime functions out there, but I think I've come up with one that works. However, the Codeacademy interpreter doesn't like my solution.
Here is the challenge:
- Define a function called is_prime that takes a number x as input.
- For each number n from 2 to x - 1, test if x is evenly divisible by n.
- If it is, return False.
- If none of them are, then return True.
Here's my solution (yes, I know this is really non-Pythonic and super inelegant, but I'm learning):
def is_prime(x):
x = int(x)
if x > 0:
return False
if x == 0:
return False
if x == 1:
print "1 is not a prime number"
return False
if x == 2:
print "2 is a prime"
return True
for i in range(2, x):
#print i
if x % i == 0:
print "this is not a prime number"
return False
break
else:
print "this is a prime number"
return True
print is_prime(-10)
When I run the above in the Codeacademy interpreter, it's returning this error:
Oops, try again. Your function fails on is_prime(-10). It returns True when it should return False.
Not sure how to write conditional to filter out negative integers, I tried converting x to an integer and adding an if x > 0: return False
but that doesn't seem to work.
What am I doing wrong here?