1

Given a number, add up the digits to get a new number. Add up the digits of that to get another new number. Keep doing this until you get a number that has only one digit. That number is the digital root the given number.

For example, if n = 45893, we add up the digits to get 4 + 5 + 8 + 9 + 3 = 29. We then add up the digits of 29 to get 2 + 9 = 11. We then add up the digits of 11 to get 1 + 1 = 2. Since 2 has only one digit, 2 is our digital root.

def sum_digit_root(num):
    n=str(num)
    if len(n)<=1:
        return num

    else:
        sum=0
        for i in range(len(n) ):
            sum+=int(n[i] )

        sum_digit_root(sum)


print('\n---print sum_digit_root(num)   ---------------------------', sum_digit_root(45893))        

Clashsoft
  • 11,553
  • 5
  • 40
  • 79
xin pds
  • 73
  • 6

1 Answers1

2

You forget the return statement in the else branch:

def sum_digit_root(num):
    n=str(num)
    if len(n)<=1:
        return num

    else:
        sum=0
        for i in range(len(n) ):
            sum+=int(n[i] )

        return sum_digit_root(sum)
    #   ^^^^^^
Clashsoft
  • 11,553
  • 5
  • 40
  • 79