Python-递归错误: maximum recursion depth exceeded in comparison error

标签 python

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

RecursionError: maximum recursion depth exceeded in comparison

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

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

我期待的结果。

enter image description here

最佳答案

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

Rule no #1:

您必须始终有一些无需递归即可解决的基本情况。

Rule no #2:

对于要递归解决的情况,递归调用必须始终是朝着基本情况取得进展的情况。

Here is your solution for recursive approach:

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))

output:

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

关于Python-递归错误: maximum recursion depth exceeded in comparison error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47482942/

相关文章:

python - pandas 有条件地按组移动或向前填充另一列

python - 图像处理算法设计 : how to get front images from an angled camera?

python - 使用 lxml 将一个 xml 复制到 python 中的另一个

javascript - JavaScript 代码中的三明治模式

python - 在 python 的记录器中分离 stdout 和 stderr

python - interp2(X, Y, Z, XI, YI) 从 Matlab 到 Python

python - 将子文件夹中的多个文件复制到一个文件夹中

python - 在 numpy 上绘制 2D 高斯分布

python 全局变量 : import vs. execfile

python - 进行类别不平衡正则化的正确位置(数据级别或批处理级别)