<p>我被困在这个练习中</p>
<p>任务:</p>
<blockquote>
<p>A digital root is the recursive sum of all the digits in a number. 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. This is only applicable to the natural numbers.</p>
</blockquote>
<p>下面是它的工作原理:</p>
<p>数字根(16)<br/>
1+6=7</p>
<p>数字根(942)<br/>
9 + 4 + 2 = 15 ... 1+5=6</p>
<p>我的方法就在这里。关于如何正确返回正确值的提示?我将感谢任何帮助</p>
<pre><code>def digital_root(n):
answ = 0
s = 0
x = str(n)
for i in range(0, len(x)):
s = s + int(x[i])
if len(str(s)) > 1:
digital_root(s)
elif len(str(s)) == 1:
answ = s # here is an answer
return answ # answer is 2 but this one is not returning 2, why?
return answ # here it returns answ=0, I want to return 2...
print(digital_root(493193))
</code></pre>