r/learnpython • u/Confident-Acadia4217 • 10m 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
1
Upvotes
•
u/DHTechdev 7m ago
A full tree needs recursion instead of a while True loop. Write a function that takes a size and a depth, draws one square, then calls itself twice for the two smaller squares on top, each with side size divided by 1.414, one turned left 45 degrees and one turned right. Stop when depth hits 0. After each branch, send the turtle back to where it started so the second branch begins in the right spot.