-1
def fact(n, summ):
    if n == 0:
        print(summ)  -- Prints 55
        return summ
    fact(n-1, summ + n)


print(fact(10, 0))  -- Output None
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Dhruv
  • 31
  • 5

1 Answers1

1

You need to return fact(n-1, summ + n) as a returning value. If a function does not return a value then it defaults to returning None.

def fact(n, summ):
    if n == 0:
        return summ
    return fact(n-1, summ + n)

print(fact(10, 0))

This outputs:

55

On a side note, your fact function could be re-implemented without the second parameter. The following produces the same output:

def fact(n):
    if n == 0:
        return 0
    return n + fact(n-1)

print(fact(10))
blhsing
  • 91,368
  • 6
  • 71
  • 106