【问题标题】:Unable to understand this recursive turtle python code无法理解这个递归的海龟 python 代码
【发布时间】:2016-08-07 06:15:21
【问题描述】:

这是我第一次提出问题,希望你们中的一些人能抽出时间来回答。

所以我的目标是使用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”的第二和第三段:为什么它们会被执行?如果函数不断重复,它永远不会正常到达那个点! 我确定我在这里遗漏了一些非常简单的东西,我希望你们都理解我的问题:) 再次感谢!

【问题讨论】:

  • 这一切都与depth 变量有关。第一个drawTree函数的深度为0,所以if语句为真,drawTree被调用深度为1,这也是真的...一直到drawTree为调用深度为11,则if语句为假,不再调用drawTree,函数返回,继续执行t.forward...深度10
  • 但是在深度为11的情况下,当if语句变为false时,不应该完全退出函数吗?无论如何,非常感谢您的意见!
  • 西蒙说了什么。 drawTree 不会永远调用自己,所以最终递归调用 do 之后的语句会被执行。当 depth == LIMIT 的递归调用返回时,控制权将返回到前一个调用,其中 depth == LIMIT-1。等等。我建议您尝试使用LIMIT = 4 运行您的代码,(或其他一些小的值)使用print(' '*depth, depth, size)if 下方。

标签: python if-statement turtle-graphics fractals


【解决方案1】:

drawTree 函数不会永远调用自己,所以最终递归调用 do 之后的语句会被执行。当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

【讨论】:

    【解决方案2】:

    例如,“深度”为 10,程序在第一段中调用 drawTree(size * SCALAR, 10 + 1)。 “depth”变为11,IF为假,程序返回drawTree,其中“depth”为10。然后程序执行下一行,第二段的第一行。

    同样的方法,程序在第二段调用drawTree(),当“深度”没有达到LIMIT,然后返回到第三段的第一行。

    【讨论】:

      猜你喜欢
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      相关资源
      最近更新 更多