编写递归函数但得到无限循环

use*_*511 0 python recursion loops image

我正在使用Python,我的任务是编写一个递归循环.想法是形成一个垂直行的图片:

1st row: 1 object  (2**0)
2nd row: 2 object  ( 2**1)
3rd row: 4 obj      (2**2)
4th:     8 obj       (2**3)
Run Code Online (Sandbox Code Playgroud)

等等直到n行和n列.

通过使用"堆栈"和"旁边"函数,我需要制定该模式.

我的代码是:

def fractal(Pattern, n):
    if n== 1:
        return beside( Pattern, n)
    else:
        return beside( fractal(Pattern, n-1), fractal(Pattern, n))
Run Code Online (Sandbox Code Playgroud)

但是,当我想显示整体模式时,会出现无限循环.

Wol*_*olf 7

你在fractal(Pattern, n)里面打电话fractal(Pattern, n).它永远不会停止这样做(至少直到它耗尽堆栈).