Sunday, August 30, 2020

write a python program for factorial using recursion - check types

 def factorial (n):

    if not isinstance(n, int):

        print('Factorial is only defined for integers.')

        return None

    elif n < 0:

         print('Factorial is not defined for negative integers.')

         return None

    elif n == 0:

         return 1

    else:

         return n * factorial(n-1)

factorial('fred')

factorial(-2)

No comments:

Post a Comment