[英]Layer Python Sum of Digits
this is my first question around here.这是我在这里的第一个问题。
I've been working on an algorithm that adds the numbers entered until I get a single-digit number, but haven't been able to find a solution for about a week.我一直在研究一种算法,该算法将输入的数字相加,直到我得到一个个位数的数字,但大约一个星期以来一直无法找到解决方案。
for example:例如:
value = 1994
Try this:尝试这个:
def func(x):
while x >= 10:
x = sum([int(c) for c in str(x)])
return x
print(func(1994))
value = 1994
while len(str(value)) != 1:
value = sum([int(i) for i in str(value)])
print(value)
5
What's going on?这是怎么回事?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.