Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Function returning None for recursive function for factorial function

I’m trying to write a code that returns the factorial of the input num but this code is returning "None" when I run it, I’m not sure why.

def factorial(num):
    if type(num) is not int():
        return None
    elif num == 0:
        return 1
    elif num < 0:
        return None
    else:
        return num * factorial(num - 1)

factorial5 = factorial(5)
print(factorial5)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You have an extra parenthesis .. see the correction from int() to int:

def factorial(num):
    if type(num) is not int:
        return None
    elif num == 0:
        return 1
    elif num < 0:
        return None
    else:
        return num * factorial(num - 1)

factorial5 = factorial(5)
print(factorial5)

NOTE: ran with Python3

120
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading