the man February 2016

Why is this function with a return value returning none?

Ok, so here is my code:

def is_prime(n):
        n = abs(int(n)) 
        if n < 2:
            return False
        elif n == 2:
            return True
        elif    n%2 == 0:
            return False
        else:
             prime(n)



    def prime(n):
        for x in range(3, int(n**0.5)+1,2):
            if n%x == 0:
                return False
            else: 
                return True

    print is_prime(6577)

But whenever I run this in my shell it returns 'None', I don't understand why. Any help would be appreciated.

Answers


user2341963 February 2016

Your final else in is_prime returns nothing. You can even remove the else altogether, but that's just personal preference

def is_prime(n):
    # You don't really need to take abs value cause you already do a check for < 2 which includes negative numbers
    n = abs(int(n))
    if n < 2:
        return False
    elif n==2:
        return True
    elif n%2 == 0:
        return False
    return prime(n)

Post Status

Asked in February 2016
Viewed 1,022 times
Voted 8
Answered 1 times

Search




Leave an answer