我是python初学者,通过the following challenge工作:
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.
下面是我的代码:
n = 132189
def kata(num):
import re
total = 0
while total >= 0 and total < 10:
for i in str(num).split():
if int(i) > 9:
split_num = (" ".join(str(i)))
running_list = []
running_list.append(split_num)
num_list = re.findall(r'\d+', str(running_list))
for i in num_list:
if i.isdigit():
total += int(i)
print(total)
kata(n)
为什么我的代码没有循环回for循环并继续分解两位数的值
main()
你的代码有很多问题。我将在下面评论这些问题并提供一个简单有效的解决方案
代码分析
溶液
此解决方案将在每次迭代中打印总数,以便您可以检查它在做什么。
1+3+2+1+8+9=24,然后2+4=6
所以它应该打印24,然后打印6
卡塔(132189); 产出:6
编辑:我不知道为什么我不能在这里正确缩进我的代码,但是你得到了答案,也试着学习如何调试,这样你就可以发现你的代码出了什么问题
相关问题 更多 >
编程相关推荐