我的Python代码包含递归函数有什么问题?

时间:2021-08-12 01:59:56

I am using recursive to find a happy number.

我正在使用递归来找到一个快乐的数字。

The following is my Python code:

以下是我的Python代码:

deepth = 0
def is_happy_number(number):
    astring = str(number)
    global deepth
    digits = [int(char) for char in astring]
    sum_digit = sum([digit**2 for digit in digits])
    if sum_digit == 1:
        deepth = 0
        return True
    else:
        deepth += 1
        if deepth >800:
            return False
    return is_happy_number(sum_digit)

print '7',is_happy_number(7)
for number in range(1,11):
    print number,is_happy_number(number)

The results are :

结果是:

7 True
1 True
2 False
3 False
4 False
5 False
6 False
7 False
8 False
9 False
10 True

When I test number 7 alone, it returns 'True'. While I run the last two raws code, number 7 return 'False' .

当我单独测试7号时,它返回'True'。当我运行最后两个原始代码时,数字7返回'False'。

I don't know which part is wrong.

我不知道哪个部分是错的。

After a few minutes, I find the wrong part in my Python code. And I add:

几分钟后,我在Python代码中找到了错误的部分。我补充说:

deepth = 0

after:

if deepth > 800:

With the remind of @Will, I find another solution to this problem. Code modified is as following:

随着@Will的提醒,我找到了另一个解决这个问题的方法。修改代码如下:

def is_happy_number(number, deepth=0):
    astring = str(number)
    digits = [int(char) for char in astring]
    sum_digit = sum([digit**2 for digit in digits])
    if sum_digit == 1:
        return True
    else:
    deepth += 1
    if deepth >800:
        return False
    return is_happy_number(sum_digit,deepth)

print '7',is_happy_number(7,0)
for number in range(1,10):
    if is_happy_number(number,0):
        print number,

3 个解决方案

#1


8  

You're failing to reset the global variable depth. A better way to deal with this is to pass the depth into the recursive call.

您无法重置全局变量深度。处理此问题的更好方法是将深度传递给递归调用。

Something like this:

像这样的东西:

def is_happy_number(number, depth=0):
    # ... as before ...
    return is_happy_number(sum_digit, depth)

#2


6  

As Barak Manos has pointed out in his answer, the deepth variable is the culprit here. It is not reset in the case where a depth of 800 is reached. If that is done, your code works:

正如巴拉克马诺斯在他的回答中指出的那样,深度变量就是罪魁祸首。在达到800的深度的情况下不重置。如果这样做,您的代码将起作用:

deepth = 0

def is_happy_number(number):
    astring = str(number)
    global deepth
    digits = [int(char) for char in astring]
    sum_digit = sum([digit**2 for digit in digits])
    if sum_digit == 1:
        deepth = 0
        return True
    else:
        deepth += 1
        if deepth >800:
            deepth = 0
            return False

    return is_happy_number(sum_digit)

print '7',is_happy_number(7)
for number in range(1,11):
    print number,is_happy_number(number)

I totally agree with Will that you shouldn't use a global variable.

我完全同意威尔你不应该使用全局变量。

#3


0  

The problem comes from the fact that you define deepth only once. Then, it's previous value is reused. To solve this, you must set deepth to 0 when you return False or True.

问题来自于您只定义深度一次的事实。然后,它的先前值被重用。要解决此问题,必须在返回False或True时将deepth设置为0。

#1


8  

You're failing to reset the global variable depth. A better way to deal with this is to pass the depth into the recursive call.

您无法重置全局变量深度。处理此问题的更好方法是将深度传递给递归调用。

Something like this:

像这样的东西:

def is_happy_number(number, depth=0):
    # ... as before ...
    return is_happy_number(sum_digit, depth)

#2


6  

As Barak Manos has pointed out in his answer, the deepth variable is the culprit here. It is not reset in the case where a depth of 800 is reached. If that is done, your code works:

正如巴拉克马诺斯在他的回答中指出的那样,深度变量就是罪魁祸首。在达到800的深度的情况下不重置。如果这样做,您的代码将起作用:

deepth = 0

def is_happy_number(number):
    astring = str(number)
    global deepth
    digits = [int(char) for char in astring]
    sum_digit = sum([digit**2 for digit in digits])
    if sum_digit == 1:
        deepth = 0
        return True
    else:
        deepth += 1
        if deepth >800:
            deepth = 0
            return False

    return is_happy_number(sum_digit)

print '7',is_happy_number(7)
for number in range(1,11):
    print number,is_happy_number(number)

I totally agree with Will that you shouldn't use a global variable.

我完全同意威尔你不应该使用全局变量。

#3


0  

The problem comes from the fact that you define deepth only once. Then, it's previous value is reused. To solve this, you must set deepth to 0 when you return False or True.

问题来自于您只定义深度一次的事实。然后,它的先前值被重用。要解决此问题,必须在返回False或True时将deepth设置为0。