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
  SQLite Query in Python rowyourboat 2 437 Apr-26-2019, 02:24 PM
Last Post: Larz60+
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 1,305 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 608 Jan-04-2019, 02:44 AM
Last Post: SheeppOSU
  Python 3.6 dict key iteration order insearchofanswers87 7 1,684 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,092 Feb-03-2018, 08:41 PM
Last Post: Larz60+
  Extract SQL Query Results in Python boobalganesan 3 3,299 Jan-11-2018, 07:31 PM
Last Post: buran
  python variable in SQL query takaa 5 18,589 Nov-23-2017, 10:12 AM
Last Post: takaa
  python turtle module in pycharm error sajley 2 6,799 Dec-12-2016, 08:52 PM
Last Post: sajley
  python!NameError: name 'Turtle' is not defined shaywune 2 10,652 Sep-24-2016, 05:23 PM
Last Post: shaywune

Forum Jump:


Users browsing this thread: 1 Guest(s)