Python - RecursionError:比较错误超出了最大递归深度

时间:2017-11-25 05:34:57

标签: python

我为一个简单的任务编写了一个简单的代码,但我不断收到错误消息

  

RecursionError:比较超出最大递归深度

我已经花了很多时间研究它,但似乎无法弄明白。

def printMultiTable(high):
    i = 1
    while i <= high:
        printMultiTable(i)
        i +=1
printMultiTable(7)

我期待的结果。

enter image description here

1 个答案:

答案 0 :(得分:1)

如果你想通过递归来解决这个问题,那么你必须要了解一些递归规则:

  

规则#1:

你必须总是有一些基本案例,可以在没有递归的情况下解决。

  

规则#2:

对于要递归求解的情况,递归调用必须始终是一个向基本情况前进的情况。

  

以下是递归方法的解决方案:

def printMultiTable(high,low,track):
multitable=[]

if track==0:
    return 0
else:
    while high > low:
        multitable.append(high*track)



        high -= 1
    print("multitable of {}".format(track))
    print('--------------------------')


    print(multitable[::-1])
    print('--------------------------')

    high=7

    printMultiTable(high,low,track-1)

打印(printMultiTable(7,0,6))

  

输出:

multitable of 6
--------------------------
[6, 12, 18, 24, 30, 36, 42]
--------------------------
multitable of 5
--------------------------
[5, 10, 15, 20, 25, 30, 35]
--------------------------
multitable of 4
--------------------------
[4, 8, 12, 16, 20, 24, 28]
--------------------------
multitable of 3
--------------------------
[3, 6, 9, 12, 15, 18, 21]
--------------------------
multitable of 2
--------------------------
[2, 4, 6, 8, 10, 12, 14]
--------------------------
multitable of 1
--------------------------
[1, 2, 3, 4, 5, 6, 7]
--------------------------
None