r/learnpython • • 9d ago

pythagoras tree

I made code for one "branch" of the Pythagoras tree, and I have no idea how to make the full tree. Can someone help? My code:

from turtle import*
x=100
while True:
    for i in range (4):
        fd(x)
        rt(90)
    rt(90)
    fd(x)
    lt(45)
    xNaDwa= x*x/2
    y= xNaDwa**0.5
    x=y
3 Upvotes

5 comments sorted by

View all comments

1

u/Free-Good9203 9d ago

the trick with `turtle` recursion is saving `pos = t.pos()` and `ang = t.heading()` right after drawing a square, then restoring them with `t.penup(); t.goto(pos); t.setheading(ang); t.pendown()` before starting the second branch. scale each child side by `size / (2 ** 0.5)` and stop once `depth == 0` so it terminates cleanly.