Is_prime : What's wrong with my code?


#1

I don’t even get an error message. The red cross appears with no explanation as to why it’s failing.



def is_prime(n):
  if n<2:
    return False
  else:
    i=2
    while (n%i != 0):
      i += 1
    
  return i==n



#2

i reset the exercise, paste your code and it ran fine and the exercise approved

ideally i think its best to return boolean values (true and false) depending on if its a prime number or not, i think this can be simply achieved by changing i==n into an if/else statement


#3

I did that and it worked. I have no idea what might have happened, but thanks.

As to returning booleans, I just think the code looks more elegant like this. This is called linear search if I’m not mistaken.


#4

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