Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Python Turtle and order of implementation query
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()

#Recursive draw, where x is length
def draw(x):

# call draw
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.
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 -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Thanks. Makes sense now

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Turtle python graphics Y0sh1 6 316 Jun-10-2020, 10:05 AM
Last Post: DPaul
  Python mysql query help please tduckman 4 531 Mar-13-2020, 03:42 PM
Last Post: Marbelous
  For loops help, using python turtle SemiBeginnerPY 2 528 Mar-10-2020, 10:46 AM
Last Post: SemiBeginnerPY
  Python Turtle Help codinghero 1 561 Jan-04-2020, 12:46 AM
Last Post: ichabod801
  SQLite Query in Python rowyourboat 2 736 Apr-26-2019, 02:24 PM
Last Post: Larz60+
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 2,850 Feb-06-2019, 01:25 AM
Last Post: woooee
  Help! Turtle not working, even when we click the turtle demo in IDLE nothing happens. BertyBee 3 1,413 Jan-04-2019, 02:44 AM
Last Post: SheeppOSU
  Python 3.6 dict key iteration order insearchofanswers87 7 2,166 May-22-2018, 05:33 PM
Last Post: snippsat
  Working with turtle graphics in python and colour slices of a pie graph... netrate 1 1,685 Feb-03-2018, 08:41 PM
Last Post: Larz60+
  Extract SQL Query Results in Python boobalganesan 3 4,574 Jan-11-2018, 07:31 PM
Last Post: buran

Forum Jump:

Users browsing this thread: 1 Guest(s)