I'm quite new to python and ive been practicing on codewars, one of the kanas is as follows;
Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. The input will be a non-negative integer.
Examples
16 --> 1 + 6 = 7
942 --> 9 + 4 + 2 = 15 --> 1 + 5 = 6
132189 --> 1 + 3 + 2 + 1 + 8 + 9 = 24 --> 2 + 4 = 6
493193 --> 4 + 9 + 3 + 1 + 9 + 3 = 29 --> 2 + 9 = 11 --> 1 + 1 = 2
my problem arises when i try to return the value. when i replace return with print, i get the answer i want, but when i use return i just get 'None'
I'll post my script bellow:
def digital_root(n):
sum = 0
for i in list(str(n)):
sum += int(i)
n = sum
if len(str(n)) > 1:
digital_root(n)
else:
print(n)
this prints the correct answer^
def digital_root(n):
sum = 0
for i in list(str(n)):
sum += int(i)
n = sum
if len(str(n)) > 1:
digital_root(n)
else:
return n
this returns none ^ unless the input value is 2 digits long