无法理解这个递归乌龟python代码 [英] Unable to understand this recursive turtle python code

查看:191
本文介绍了无法理解这个递归乌龟python代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

this is my first time asking a question, I hope some of you will find time to answer.

所以我的目标是使用turtle模块编写一个python脚本来编写毕达哥拉斯树。

So my goal is to write a python script using the turtle module to code a pythagoras tree.

I我花了几天时间,而且我真的无法超越某一点,所以我在网上寻找帮助。我发现了一个代码可以完成我想要的代码,只需要很少的代码行:

I've spent days on it, and I really couldn't advance past a certain point, so I looked online to help me. I've found a code that does what I want but with very little lines of code:

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的第二段和第三段:为什么它们会被执行?如果函数不断重复,它通常不会达到这一点!
我确定我在这里错过了一些非常简单的东西,我希望你们都明白我的问题:)再次感谢!

So I understand most of the code, except the second and the third paragraph of the "if": why do they ever get executed? If the function keeps repeating itself, it never reaches that point normally! I'm sure that I'm missing something really easy here and I hope y'all understood my question :) thanks again!

推荐答案

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

The drawTree function doesn't keep calling itself forever, so eventually the statements after the recursive call do get executed. When the recursive call at depth == LIMIT returns then control passes back to the previous call, where depth == LIMIT-1.

这是一个稍微修改过的版本你的代码带有几个 print 调用以帮助跟踪执行。

Here's a slightly modified version of your code with a couple of print calls thrown in to help trace the execution.

我也做了一个其他一些小的变化。我简化了 SCALAR 计算: 0.5 * sqrt(2) == sqrt(0.5) ,当乌龟实际上正在画一个正方形时,我只会把笔放下来。我还添加了一个 turtle.mainloop()调用,以便在绘图完成时窗口保持打开状态。

I've also made a few other minor changes. I simplified the SCALAR calculation: 0.5 * sqrt(2) == sqrt(0.5), and I only put the pen down when the turtle is actually drawing a square. I've also added a turtle.mainloop() call so that the window stays open when the drawing is finished.

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

这篇关于无法理解这个递归乌龟python代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆