i cant understand this please help.

def is_prime(x):
  for n in range(2 ,x-1):
    if x%n == 0 :
      return False
      return True


what about numbers lesser then 2? They are not prime numbers and are not handled by the loop, make a if condition to check if x is lesser then 2, if so, return False

Now, by default at the end of a function, a function returns None, if we want to return something else at the end of the function, we can use the return keyword, but this will end the function

so your loop ends in the first iteration of your loop

but for a number to be a prime number, x shouldn’t be divisible by n in range 2 till x -1, so we only should return True after the loop


A post was split to a new topic: Html


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