Search code examples
pythonturtle-graphicspython-turtle

drawing a triangle with lines across using python turtle


I am still learning and trying to draw a triangle with lines across from bottom to top, but am having a terrible time figuring it out. I am able to pick the turtle up and move it drawing slightly bigger or smaller to make several triangles enclosed in one larger one, but I am beating my head off of the table trying to figure this out. Any help is greatly appreciated. Thanks in advanceenter image description here

import turtle

t = turtle.Turtle()
def drawTriangle(t, side):
  t.forward(side)
  t.left(120)



for x in range (3):
  drawTriangle(t, 100)


drawTriangle()

Solution

  • Here is a basic triangle, if you have a function called drawTriangle then it kind of makes sense to make it draw a triangle rather than something that you have to call three times to get a triangle

    import turtle
    
    def drawTriangle(t, side):
      for _ in range(3):
        t.forward(side)
        t.left(120)
    
    t = turtle.Turtle()
    drawTriangle(t, 200)
    

    Not sure what you mean by lines across, so if you edit the question to make that clearer then hopefully I will notice in time to edit the answer to add that part - okay now I see the picture, coming up.......

    This will do.......

    import turtle
    
    def drawTriangle(t, side):
      for _ in range(3):
        t.forward(side)
        t.left(120)
    
    t = turtle.Turtle()
    t.penup()
    t.setheading(-120)
    t.setposition(0, 100)
    t.pendown()
    
    for side in range(40, 240, 40):
      drawTriangle(t, side)