我如何重复循环直到总和变成一位数字?这就是我走的距离:
numb = "123456789"
for x in numb:
sum += int(x)
print(sum)
答案 0 :(得分:1)
这应该有效
def sum_digits(integ):
integ=int(integ)
if integ <= 9:
return integ
res = sum(divmod(integ, 10))
return sum_digits(res)
答案 1 :(得分:1)
尝试以下代码段:
def sumdigi(num):
sumi = 0
for i in num:
sumi+=int(i)
if (sumi)>9:
return sumdigi(str(sumi))
return sumi
print(sumdigi("12345"))