无法理解这个递归的turtle python代码

时间:2016-04-15 10:13:54

标签: 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(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)

所以我理解大部分代码,除了“if”的第二段和第三段:为什么它们会被执行?如果函数不断重复,它通常不会达到这一点! 我确信我在这里错过了一些非常简单的东西,我希望你们都理解我的问题:)再次感谢!

2 个答案:

答案 0 :(得分:0)

For example, "depth" is 10 and program called drawTree(size * SCALAR, 10 + 1) in first paragraph. "depth" becomes 11, IF is false and program returns back to drawTree, where "depth" is 10. And then program executes next line, first line in second paragraph.

In exactly the same way, program called drawTree() in second paragraph, while "depth" not reaches LIMIT, then returns back and go to first line of third paragraph.

答案 1 :(得分:0)

drawTree函数不会永远调用自身,因此最终会在递归调用执行之后执行语句。当depth == LIMIT的递归调用返回时,控件会返回上一个调用,其中depth == LIMIT-1

这里是您的代码的略微修改版本,其中引入了几个print调用以帮助跟踪执行。

我还做了一些其他的小改动。我简化了SCALAR计算: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()

<强>输出

 root 0 start
     left  1 start
         left  2 start
             left  3 start
             left  3 stop
             right 3 start
             right 3 stop
         left  2 stop
         right 2 start
             left  3 start
             left  3 stop
             right 3 start
             right 3 stop
         right 2 stop
     left  1 stop
     right 1 start
         left  2 start
             left  3 start
             left  3 stop
             right 3 start
             right 3 stop
         left  2 stop
         right 2 start
             left  3 start
             left  3 stop
             right 3 start
             right 3 stop
         right 2 stop
     right 1 stop
 root 0 stop