0

Recursive Py Program Returning j=None when j==1? This does not make sense as base case specified j must equal 1 and does not call function again.

import sys
y=10
def decrease(j):
    if j==1:
        print('j =' + str(j) + '(1)')
        print('returning j')
        return j
    else:
        print('j =' + str(j) + '(not 1)')
        print('decreasing j')
        j = j-1
        print('calling decrease j')
        decrease(j)
y=decrease(y)
print('complete')
print(y)

1 Answers1

1

You forget to return decrease(j) at the end of the second branch.

Usually when you encounter unexpected None returned from function, check first that all the branches end with a return statement. Without it, the function returns None

doraemon
  • 2,296
  • 1
  • 17
  • 36