0

Heyo!

This is kind of a weird issue that I'm having. My functions goal is to repeat N number k times, add those digits together and then proceed to add those digits until the length of the digit is 1.

This is the function:

def md(N,k):
    p = list()
    for i in range(0,k):
        p.append(list(map(int,str(N))))
        to_add = list(itertools.chain(*p))
    print ('List made:', to_add)
    result = sum(to_add)
    print ('Result found:', result, 'len:', len(str(result)))

    if len(str(result)) != 1:
        print ('Entering recursion...')
        md(result,1)
    else:
        print ('I should return:',result)
        ans = result
        return ans

The output of the function is as follows:

List made: [3, 2, 1, 3, 2, 1, 3, 2, 1]
Result found: 18 len: 2
Entering recursion...
List made: [1, 8]
Result found: 9 len: 1
I should return: 9

As you can see, the result is the answer I'm looking for. Result is defined in the else statement and is able to be printed. However, the return statement is simply returning None. We don't enter the recursive case again and I am rather stumped.

Thanks for looking!

Tom Slesinger
  • 59
  • 1
  • 8

1 Answers1

3

Some parts of your function are not returning a value:

if len(str(result)) != 1:
    print ('Entering recursion...')
    return md(result,1)    #<----  here
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80