Unable to understand this recursive turtle python code

我是研究僧i 提交于 2019-11-28 10:36:14

问题


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

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

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)

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!


回答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.

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

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()

output

 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:


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.



来源:https://stackoverflow.com/questions/36644336/unable-to-understand-this-recursive-turtle-python-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!