功能正在返回'无'同时计算“添加持久性”#39;一个数字

时间:2017-10-21 09:55:14

标签: python python-3.x

请使用此打印声明正确计算' Count'这就是我要回来的东西,但最终我得到了“没有”#39;结果是。 请指出必须使函数提供正确的计数值的校正...

def additive_persistence(num, count):
    sumer = 0
    num_copy = num

    while num_copy:
        sumer = sumer + (num_copy % 10)
        num_copy = num_copy // 10
    count += 1
    print(sumer, count)      # just to check the calculation is right!
    if sumer > 9:
        num = sumer
        additive_persistence(num, count)

    else:    
        return (count)

counter = 0    
number = int(input("Enter Number: "))
add_pers = additive_persistence(number, counter)
print("Additive Persistence of number {}, is: {}".format(number, add_pers))

2 个答案:

答案 0 :(得分:2)

你未能正确递归。

  return additive_persistence(num, count)

答案 1 :(得分:0)

您需要在递归函数中调用函数并返回其值:

   while num_copy:
        sumer = sumer + (num_copy % 10)
        num_copy = num_copy // 10
    count += 1
    print(sumer, count)      # just to check the calculation is right!
    if sumer > 9:
        num = sumer
        return additive_persistence(num, count)