无法理解此递归python代码
这是我第一次问问题,我希望你们中的一些人能抽出时间回答 因此,我的目标是使用turtle模块编写一个python脚本来编写一棵毕达哥拉斯树 我花了好几天的时间在这上面,但我真的无法超越某一点,所以我上网寻求帮助。我发现了一个代码,可以实现我想要的功能,但代码行很少:无法理解此递归python代码,python,if-statement,turtle-graphics,fractals,Python,If Statement,Turtle Graphics,Fractals,这是我第一次问问题,我希望你们中的一些人能抽出时间回答 因此,我的目标是使用turtle模块编写一个python脚本来编写一棵毕达哥拉斯树 我花了好几天的时间在这上面,但我真的无法超越某一点,所以我上网寻求帮助。我发现了一个代码,可以实现我想要的功能,但代码行很少: import turtle t = turtle.Pen() LIMIT =11 SCALAR = 0.5 * (2 ** 0.5) def drawTree(size, depth): drawSquare
import turtle
t = turtle.Pen()
LIMIT =11
SCALAR = 0.5 * (2 ** 0.5)
def drawTree(size, depth):
drawSquare(size)
if depth + 1 <= LIMIT:
t.left(90)
t.forward(size)
t.right(45)
drawTree(size * SCALAR, depth + 1)
t.forward(size * SCALAR)
t.right(90)
drawTree(size * SCALAR, depth + 1)
t.left(90)
t.backward(size * SCALAR)
t.left(45)
t.backward(size)
t.right(90)
def drawSquare(sideLength):
for i in range(4):
t.forward(sideLength)
t.left(90)
t.up(); t.goto(-100, -200); t.down()
drawTree(170.0, 0)
导入海龟
t=海龟。钢笔()
极限=11
标量=0.5*(2**0.5)
def绘图树(大小、深度):
drawSquare(尺寸)
例如,如果深度+1,则“深度”为10,程序在第一段中称为drawTree(大小*标量,10+1)。“深度”变为11,若为false,程序返回drawTree,其中“深度”为10。然后程序执行下一行,第二段的第一行
以完全相同的方式,程序在第二段中调用drawTree(),而“深度”未达到限制,然后返回并转到第三段的第一行。例如,“深度”为10,程序在第一段中调用drawTree(大小*标量,10+1)。“深度”变为11,若为false,程序返回drawTree,其中“深度”为10。然后程序执行下一行,第二段的第一行
以完全相同的方式,程序在第二段中调用了drawTree(),而“深度”没有达到限制,然后返回并转到第三段的第一行。drawTree
函数不会永远保持自身调用,因此递归调用后的语句最终会执行。当depth
=LIMIT
处的递归调用返回时,控制传递回上一个调用,其中depth
=LIMIT-1
这里是一个稍微修改过的代码版本,其中包含两个print
调用,以帮助跟踪执行情况
我还做了一些其他的小改动。我简化了标量的计算:0.5*sqrt(2)
=sqrt(0.5)
,我只在海龟实际画正方形时才放下笔。我还添加了一个turtle.mainloop()
调用,以便在绘图完成时窗口保持打开状态
from __future__ import print_function
import turtle
LIMIT = 3
SCALAR = 0.5 ** 0.5
INDENT = ' ' * 4
def drawTree(size, depth, branch):
print(INDENT * depth, branch, depth, 'start')
drawSquare(size)
if depth + 1 <= LIMIT:
t.left(90)
t.forward(size)
t.right(45)
drawTree(size * SCALAR, depth + 1, 'left ')
t.forward(size * SCALAR)
t.right(90)
drawTree(size * SCALAR, depth + 1, 'right')
t.left(90)
t.backward(size * SCALAR)
t.left(45)
t.backward(size)
t.right(90)
print(INDENT * depth, branch, depth, 'stop')
def drawSquare(sideLength):
t.down()
for i in range(4):
t.forward(sideLength)
t.left(90)
t.up()
t = turtle.Pen()
t.up()
t.goto(-100, -200)
drawTree(100.0, 0, 'root')
turtle.mainloop()
drawTree
函数不会一直调用自己,因此最终会执行递归调用后的语句。当depth
=LIMIT
处的递归调用返回时,控制传递回上一个调用,其中depth
=LIMIT-1
这里是一个稍微修改过的代码版本,其中包含两个print
调用,以帮助跟踪执行情况
我还做了一些其他的小改动。我简化了标量的计算:0.5*sqrt(2)
=sqrt(0.5)
,我只在海龟实际画正方形时才放下笔。我还添加了一个turtle.mainloop()
调用,以便在绘图完成时窗口保持打开状态
from __future__ import print_function
import turtle
LIMIT = 3
SCALAR = 0.5 ** 0.5
INDENT = ' ' * 4
def drawTree(size, depth, branch):
print(INDENT * depth, branch, depth, 'start')
drawSquare(size)
if depth + 1 <= LIMIT:
t.left(90)
t.forward(size)
t.right(45)
drawTree(size * SCALAR, depth + 1, 'left ')
t.forward(size * SCALAR)
t.right(90)
drawTree(size * SCALAR, depth + 1, 'right')
t.left(90)
t.backward(size * SCALAR)
t.left(45)
t.backward(size)
t.right(90)
print(INDENT * depth, branch, depth, 'stop')
def drawSquare(sideLength):
t.down()
for i in range(4):
t.forward(sideLength)
t.left(90)
t.up()
t = turtle.Pen()
t.up()
t.goto(-100, -200)
drawTree(100.0, 0, 'root')
turtle.mainloop()
这都是关于
深度
变量的。第一个drawTree
函数的深度为0,因此if
语句为true,调用drawTree
的深度为1
,这也是true。。。一直到调用深度为11
的drawTree
为止,如果语句为false,则不再调用drawTree
,函数返回,继续执行深度为10的t.forward…
向前,但当if语句变为false时,深度为11,它不应该完全退出函数吗?无论如何,非常感谢你的投入!西蒙说的drawTree
不会一直调用自身,因此最终会执行递归调用后的语句。当depth==LIMIT处的递归调用返回时,控制传递回上一个调用,其中depth==LIMIT-1。等等。我建议你试着用LIMIT=4运行你的代码,用print(''*depth,depth,size)
在if
下运行你的代码。这都是关于depth
变量的。第一个drawTree
函数的深度为0,因此if
语句为true,调用drawTree
的深度为1
,这也是true。。。一直到调用深度为11
的drawTree
为止,如果语句为false,则不再调用drawTree
,函数返回,继续执行深度为10的t.forward…
向前,但当if语句变为false时,深度为11,它不应该完全退出函数吗?无论如何,非常感谢你的投入!西蒙说的drawTree
不会一直调用自身,因此最终会执行递归调用后的语句。当depth==LIMIT处的递归调用返回时,控制传递回上一个调用,其中depth==LIMIT-1。等等。我建议您尝试使用LIMIT=4运行代码,使用print(''*depth,depth,size)
在if
下运行代码(或其他小值)。