is_Prime while loop


#1

Hi All,

Could someone help me with the below code?

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

I assume it is wrong, although I don’t receive any error message, it just keeps running…

Thanks for the help!


#2

i certainly get an error message while running your code:

  File "python", line 7
    elif abs(x) <= 1:
       ^
SyntaxError: invalid syntax

while/else exist in python, but while/elif is impossible. Maybe you could check if x is lesser then 2 before the loop?


#3

After resetting the workspace and adjusting it worked. Thanks so much!!


#4

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