0
def fact(n):
    if n<0:
        return 0
    if (n==0 or n==1):
        return 1
    else:
        n * fact(n-1)

n = int(input("Enter a number"))
fact(n)

i dont know why its giving me a TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'

Dave Costa
  • 47,262
  • 8
  • 56
  • 72

2 Answers2

0

You failed to return a value from your recursion case:

    else:
        n * fact(n-1)

You need

    else:
        return n * fact(n-1)

The default return value is None, so you end up trying to multiply None by n at every level above the second from the bottom.

For instance, when you try fact(3), you recur ...

3 * fact(2)
  2 * fact(1)
    fact(1) is 1 ... return that ...
  2 * 1 is 2 ... but you then return None ...
3 * None ... is illegal
Prune
  • 76,765
  • 14
  • 60
  • 81
0

I believe you meant

return n * fact(n-1)
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116