I'm working through an algorithm and wanted to try to use recursive functions to solve it. In doing so I have this code:
def persistence(n, count=0):
holder = 1
if n < 9:
return count
num = [int(a) for a in str(n)]
print(f'num {num}')
for i in num:
holder = holder * i
print(f'holder {holder}')
count += 1
print(f'count {count}')
if holder < 9:
print(f'The final count is {count}')
return count
else:
persistence(holder, count)
persistence(39)
The problem is the return in the If statement never gets called when the criteria is met:
if holder < 9:
print(f'The final count is {count}')
return count
else:
persistence(holder, count)
It will run the print but never run the return and instead recalls the functions until nothing is returned.