【发布时间】:2014-02-06 02:38:07
【问题描述】:
我正在使用 Python,我的任务是编写一个递归循环。这个想法是形成一个有垂直行的图片:
1st row: 1 object (2**0)
2nd row: 2 object ( 2**1)
3rd row: 4 obj (2**2)
4th: 8 obj (2**3)
以此类推,直到 n 行和 n 列。
通过使用 'stack' 和 'beside' 函数,我需要制定该模式。
我的代码是:
def fractal(Pattern, n):
if n== 1:
return beside( Pattern, n)
else:
return beside( fractal(Pattern, n-1), fractal(Pattern, n))
但是,当我想展示整体格局时,却出现了无限循环。
【问题讨论】:
-
您一定缺少基本情况。确定无需再次调用函数即可返回的最小单位。
-
你确定你的意思不是
return beside( fractal(Pattern, n-1), fractal(Pattern, n-2))什么的吗? -
fractal(Pattern, n)在最后一行调用fractal(Pattern, n)。那是无限递归......又名inception
标签: python image loops recursion