Recursive Function That Will Sum Digits Of Input
Trying to write a piece of code that will sum the digits of a number. Also I should add that I want the program to keep summing the digits until the sum is only 1 digit. For examp
Solution 1:
Convert back and forth between strings and ints, make use of sum().
>>> deffoo(n):
n = str(n)
iflen(n) == 1:
returnint(n)
return foo(sum(int(c) for c in n))
>>> foo(1969)
7>>>
deffoo(n):
n = str(n)
iflen(n) == 1:
returnint(n)
return foo(sum(int(c) for c in n))
Post a Comment for "Recursive Function That Will Sum Digits Of Input"