Python Forum

Full Version: making a basic tree help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm new to python and trying to make a basic tree, I have made this code to draw the triangles on top of each other but I am unable to work out how to make them larger as they go down. Can any one help?

number_of_shapes = 3
for shape in range(1, number_of_shapes + 1):
	# Draw a Triangle
    for sides in range(1, 4):
        forward(60)
        left(120)
    # Move forward to start position of next triangle
    penup()
    right(120)
    forward(60)
    left(120)
    forward(30)
    pendown()
Thanks

Charley
Can you share a working example we can look at? There's at least something you're importing, right?
Hi,

The only thing I'm importing is

from turtle import *
full code

from turtle import *
number_of_shapes = 3
for shape in range(1, number_of_shapes + 1):
    # Draw a Triangle
    for sides in range(1, 4):
        forward(60)
        left(120)
    # Move forward to start position of next triangle
    penup()
    right(120)
    forward(60)
    left(120)
    forward(30)
    pendown()
This makes 3 triangles stacked on top of each other.

Charley

I can do it this way but I was hope to shorten the code.

from turtle import *
# Draw top of tree
forward(30)
left(120)
forward(60)
left(120)
forward(60)
left(120)
forward(30)
# Move forward to start position of next triangle
penup()
right(90)
forward(70)
left(90)
pendown()
# Draw middle of tree
forward(40)
left(120)
forward(80)
left(120)
forward(80)
left(120)
forward(40)
# Move forward to start position of next triangle
penup()
right(90)
forward(90)
left(90)
pendown()
# Draw bottom of tree
forward(50)
left(120)
forward(100)
left(120)
forward(100)
left(120)
forward(50)
for shape in range(1, number_of_shapes + 1):
    # Draw a Triangle
    for sides in range(1, 4):
        forward(60 * shape)
        left(120)
Try heading in this direction, where you're basing the amount of movement off of which iteration you're in.