Nikolay Grigorov
Nikolay Grigorov

Reputation: 21

i need to make a graphical representation of four triangles

I have to write a code which produce a set of triangles. 4 triangles with sides 20.40.60.80 respectively, placed diagonally.

The distance between adjacent triangles should be 10 units between the top point of the lower triangle and the lower left point of the higher triangle.

My attempt. But it doesn't work as it should. Can you please help find the mistake I made?

from turtle import *

left(60)
number_of_shapes = 4

for shape in range(0, number_of_shapes):
    for sides in range(1, 4):
        forward(20 + 20 * shape)
        right(120)

    for shape in range(0, number_of_shapes):
        penup()
        forward(30 + 20 * shape)
        pendown()

Upvotes: 2

Views: 367

Answers (1)

Rabbid76
Rabbid76

Reputation: 210968

You do not need the 2nd for loop, because you want to move the pen to the top of the triangle (+10), from the current position. The current position is the start and the end point of the triangle which was drawn before.

from turtle import *

left(60)
number_of_shapes = 4

for shape in range(0, number_of_shapes):
    for sides in range(1, 4):
        forward(20 + 20 * shape)
        right(120)

    penup()
    forward(30 + 20 * shape)
    pendown()

Upvotes: 1

Related Questions