简体   繁体   English

一个递归函数来计算数字的总和

[英]a recursive function to calculate sum of a number digits

i have to write a recursive function which calculates sum of a number digits,here's the code i tried : 我必须编写一个递归函数来计算数字的总和,这是我尝试过的代码:

def sum_digit(n):
   sum=0
   a = n % 10
   n //= 10
   sum += a
   while n > 0 :
      sum = sum + sum_digit(n)
   return sum


print(sum_digit(67154))

i don't know why i don't get the 23 as answer...my program doesn't come to an end 我不知道为什么我没有得到23的答案...我的程序还没有结束

for example 23 number(please correct me if I'm wrong,I'm a newbie in python),the 3 goes to sum, and n become2,since its > 0 then it should go to while,so now it should calculate sum digit(2),the a become 2 and 2 goes to sum and n become 0 and sum digit(2) returns 2,then it sum with the 3 and i must get 5. i appreciate your help. 例如23个数字(如果我错了,请纠正我,我是python中的新手),3求和,n变为2,因为其> 0,那么它应该到达while,所以现在应该计算总和digit(2),a变为2,2求和,n变为0,sum digit(2)返回2,然后将其与3求和,我必须得到5。我感谢您的帮助。

You have an infinite loop because n never changes within the loop. 您有一个无限循环,因为n在循环内永不改变。 Note that assigning a new value to n in the scope of the called function will not change n in the outer scope. 请注意,在被调用函数的范围内为n分配新值不会在外部范围内更改n

Also, it seems you are mixing an iterative solution with a recursive one. 另外,似乎您正在将迭代解决方案与递归解决方案混合在一起。 If you do the recursive call, you do not need the loop, and vice versa. 如果执行递归调用,则不需要循环,反之亦然。

You can either do it recursively: 您可以递归执行:

def sum_digit(n):
    if n > 0:
        return sum_digit(n // 10) + n % 10
    else:
        return 0

Or in an iterative way: 或以迭代方式:

def sum_digit(n):
    s = 0
    while n > 0:
        s += n % 10
        n //= 10
    return s

Or just using bultin functions (probably not what your teacher wants to see): 或仅使用公告功能(可能不是您的老师想要看到的):

def sum_digit(n):
    return sum(map(int, str(n)))

我必须用if来更改时间,并且它的工作原理非常感谢您的评论,并很抱歉在此处发布这样的问题。

This will do the trick: 这将达到目的:

def sum_digit(n, current_sum=0):
    if n == 0:
        return current_sum
    else:
        digit = n % 10
        current_sum += digit
        n //= 10
        return sum_digit(n, current_sum)

The output: 输出:

print(sum_digit(67154))
> 23

You were mixing up an iterative method (the while loop) and the recursive method (the function call). 您正在混合使用迭代方法(while循环)和递归方法(函数调用)。

In a recursive function, must make sure you get these things right: 在递归函数中,必须确保正确执行以下操作:

The end condition (in our case, we return the sum when the digit is 0) 结束条件(在本例中,当数字为0时,我们返回总和)

and

The recursive call (in our case, going down one digit each time) 递归调用(在我们的例子中,每次下降一位)

Change your code as following: 更改您的代码,如下所示:

def sum_digit(n):
   sum=0
   a = n % 10
   n //= 10
   sum += a
   if n > 0 :
      sum = sum + sum_digit(n)
   return sum

The reason is n is assigned new reference inside function, but it's invisible outside. 原因是n在函数内部被分配了新的引用,但在外部却不可见。 so while part is loop died. 所以当一部分循环死掉时。 In fact, while part is executed at most once, so code was changed as above. 实际上,尽管部分最多执行一次,所以代码如上所述进行了更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM