Is_prime - Works on my computer, but not in the practice window?

#1

HI all! I'm stuck on is_prime and was wondering if I could get some help.

I'm getting an infinite loop on the practice window yet the code works when I just run it on my Mac.

Running this in terminal gives me the correct answer every time. Running it on codecademy, not so much. Am I missing an infinite loop? Thanks!!

``````def is_prime(x):
if x == 1:
return False
elif x == 2:
return True
else:
n = 2
while n != x:
if x % n == 0:
return False
break
elif n + 1 == x:
return True
break
else:
n += 1

number = raw_input("Give me a number and let\'s see if it's a prime number: ")
number = int(number)
print is_prime(number)``````

#2

There is an infinite loop if x starts below 2, n will keep increasing and x can be -10 so they will never be equal.

Also, you don't need those breaks, they will never be hit.

#3

As already alluded to, this is a condition that may never NOT be met. Better in these cases to pick one side or the other to approach from.

``while n < x:``

#4

Ah, that makes sense!!! So if I understand this correctly,

n != x will in many cases always be true. For example - 0.

Once I changed this to < it worked. I also removed the breaks. I read up and returns break a loop as does a break.

Thanks everyone!!

#7