Python Forum
Python Turtle and order of implementation query
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Turtle and order of implementation query
#1
Hello all, first post here so hopefully this is in the right place.
Would be greatful iff someone could help me understand something, Im just starting to
learn python as a hobbyist and was doing some fractal tutorials with Turtle.

Came across code for a fractal tree, as bellow.
import turtle
PROGNAME = 'Fractal Tree'

myPen = turtle.Turtle()
myPen.up()
myPen.left(90)
myPen.back(300)
myPen.down()
myPen.speed(10)
myPen.color("#000000")

#Recursive draw, where x is length
def draw(x):
    if(x<5):
        return
    else:
        myPen.forward(x)
        myPen.left(30)
        draw(3*x/4)
        myPen.right(60)
        draw(3*x/4)
        myPen.left(30)
        myPen.back(x)

# call draw
draw(300)
Now I thought I understood all this untill noticing when running the programme that turtle always draws the left track untill x hits 5, then traces back and does the next left turn till again x hits 5.

I was expecting the programme to draw one y fork, then another smaller y fork comming from the left of the first and so on till x hits 5.

So i guss my question is why does python draw a single line always executing the left turn untill x hits its < cut off, instead of going through all of the commands within the else loop first?

I hope that makes sense?
Reply
#2
It' the way the recursion works. Trace it through: A x = 300, it goes forward 300 and left 30. Then it calls itself with x = 225. Control now passes to x = 225, which goes forward 225 and left 30, and calls itself with x = 169. Control passes to x = 169, which goes forward 169 and left 30, and calls itself ...

Since the recursion first occurs after drawing the left fork, it's going to draw the left fork until it gets to the terminal state. This is called depth-first, as opposed to breadth-first, which is what you were expecting.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks. Makes sense now
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python implementation of Longest Common Substring problem Bolt 0 531 Sep-17-2023, 08:31 PM
Last Post: Bolt
  list the files using query in python arjunaram 0 649 Mar-28-2023, 02:39 PM
Last Post: arjunaram
  Turtle.setpos() vs text position on screen query ElectronWrangler 0 1,507 Nov-26-2022, 02:39 AM
Last Post: ElectronWrangler
  python sql query single quote in a string mg24 1 993 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  "SUMIF" type query in Python (help required) BlainEillimatta 0 799 Oct-06-2022, 09:08 AM
Last Post: BlainEillimatta
  MSSQL query not working in Python kat35601 0 874 Apr-12-2022, 06:44 PM
Last Post: kat35601
  Error using mariadb select query with form in python? shams 2 1,955 Jul-29-2021, 12:30 PM
Last Post: shams
  Understand order of magnitude performance gap between python and C++ ThelannOryat 4 2,663 Mar-17-2021, 03:39 PM
Last Post: ThelannOryat
  What is the Python Implementation for This? pythonflea 5 2,431 Feb-08-2021, 08:18 PM
Last Post: buran
  Python 3 Turtle - Screen Events and Coords peteralien 0 1,651 Aug-18-2020, 11:25 PM
Last Post: peteralien

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020