Why return None when doing recursion?
Question:
I’m doing this problem Add Digits in recursion way. Why does it return None?
def addDigits(num):
"""
:type num: int
:rtype: int
"""
shit = str(num)
n = len(shit)
if n == 1:
return num
else:
num = 0
for i in xrange(n):
num += int(shit[i])
addDigits(num)
Answers:
You need to return the value of the recursive call in the last line:
return addDigits(num)
Without such a return
you silently return None
I’m doing this problem Add Digits in recursion way. Why does it return None?
def addDigits(num):
"""
:type num: int
:rtype: int
"""
shit = str(num)
n = len(shit)
if n == 1:
return num
else:
num = 0
for i in xrange(n):
num += int(shit[i])
addDigits(num)
You need to return the value of the recursive call in the last line:
return addDigits(num)
Without such a return
you silently return None