gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:25:13 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com