def numRoot(num):
‘‘‘定义数根函数‘‘‘
if len(num) == 1:
return int(num)
else:
nums = []
for i in range(len(num)):
# 对字符串进行遍历
nums.append(int(num[i]))
if sum(nums) >= 10:
# 如果数值加起来大于 10
numRoot(str(sum(nums)))
else:
# 输出树根
print(sum(nums))
num = input("请输入一个数,查看它的数根")
numRoot(num)
2020-05-23
原文:https://www.cnblogs.com/hany-postq473111315/p/12943740.html