1

For the code below:

su = 0
def sod(N):
    global su
    su += N%10
        if N != 0:
            N = int(N/10)
            sod(N)
    return su

print(sod(int(input())))

The result is correct. For example., for input 16 its printing 7.

But for the code below

su = 0
def sod(N):
    global su
    su += N%10
        if N != 0:
            N = int(N/10)
            sod(N)
        else:
            return su

print(sod(int(input())))

The result is unexpected. Its printing None.

Abhinav
  • 429
  • 5
  • 13
  • You should really by capturing the return value of `sod(N//10)` and summing it with `n%10` instead of using globals. – Patrick Haugh Oct 10 '18 at 13:59
  • I think it has to do with the fact that your recursive function does not return anything when `N` is different from zero... – toti08 Oct 10 '18 at 14:08

0 Answers0