Bottom Page

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?
Larz60+ wrote Mar-02-2019, 11:28 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

I fixed for you this time, please use BBcode tags on all future posts. Thank you.
Quote
#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

Quote
#3
Thanks. Makes sense now
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  SQL query with a variable amount of parameters Antares 10 462 Jul-08-2019, 02:24 PM
Last Post: Antares
  Nested while loop problem + turtle DreamingInsanity 3 165 Jul-06-2019, 02:01 PM
Last Post: DreamingInsanity
  maintaining list order 3Pinter 13 403 Jun-05-2019, 07:49 AM
Last Post: 3Pinter
  sieve of eratosthenes implementation sunny_awesome 1 138 May-24-2019, 12:38 PM
Last Post: perfringo
  Python Script to Produce Difference Between Files and Resolve DNS Query for the Outpu sultan 2 184 May-22-2019, 07:20 AM
Last Post: buran
  Sqlite3 help with descending order I_Am_Groot 4 169 May-18-2019, 12:45 AM
Last Post: I_Am_Groot
  Animation using matplotlib query JohnDoe 0 159 May-09-2019, 08:07 PM
Last Post: JohnDoe
  Add Variable to select query UtiliseIT 2 235 May-03-2019, 11:45 AM
Last Post: UtiliseIT
  SQLite Query in Python rowyourboat 2 284 Apr-26-2019, 02:24 PM
Last Post: Larz60+
  pymongo wildcard query issue MikeAW2010 1 999 Apr-09-2019, 06:32 PM
Last Post: reniformpuls

Forum Jump:


Users browsing this thread: 1 Guest(s)