Search code examples
pythonpython-3.xfunctionnonetype

IsPrime() function getting None values


Hi i created some code for an is_prime function however when i enter small values i get none instead of true/false can anyone explain to me why this is?

 def is_prime(n):
  if n <= 1:
     return False

    for i in range(2,n):
        if (n%i == 0):
            return False
    return True

Solution

  • Indent your loop properly like this: The first if and for should be in the same line.

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

    Now it works properly,

    In [652]: is_prime(5)
    Out[652]: True
    
    In [653]: is_prime(6)
    Out[653]: False
    
    In [654]: is_prime(7)
    Out[654]: True