btorralba
btorralba

Reputation: 11

Nested for Loops to Make a Triangle Pattern in Python

Basically wanting to make these two patterns, both dependent on n (number of rows for 1 and number of triangles for 2) which here is 5 for both cases. See the pictures below for the desired output.

Here is what I came up with for the first one. Draws dots going down with each iteration in the inner loop adding one dot to the left. Ideally, I want it to make the dots going left to right and then each iteration in the inner loop adding one dot up.

def drawTriangularSeries(myTurtle, n):
    sideLength = 10
    x = 0
    y= 200

    myTurtle.penup()
    myTurtle.goto(x, y)
    for row in range(n+1):
        for col in range(row):
            myTurtle.dot()
            myTurtle.back(sideLength)
        myTurtle.back(sideLength)
        y -= sideLength
        myTurtle.goto(x, y)

Here is what I did for the second one. Whenever it goes to draw the next triangle, it draws it below the previous one.

def drawTriangle(myTurtle, dotsPerSide, startX, startY):
    sideLength = 10
    x = startX
    y = startY

    myTurtle.penup()
    myTurtle.goto(x, y)
    for i in range(dotsPerSide+1):
        x += sideLength * i
        y += sideLength * i
        myTurtle.goto(x, y)
        for j in range(i + 1):
            for k in range(j):
                myTurtle.dot()
                myTurtle.back(sideLength)
            myTurtle.back(sideLength)
            y -= sideLength
            myTurtle.goto(x, y)

How do I rewrite the loops such that it makes the picture? Here is current output: Current Output Here's what it's supposed to look like, where problem one is making the last triangle and problem two is making the whole series Correct Ouput

Upvotes: 1

Views: 1027

Answers (3)

exxbrain
exxbrain

Reputation: 636

Try recursive function. Something like:

import turtle

def draw_triangle(size, col=1):
    for shift_y in range(0, col):
        my_turtle.goto(my_turtle.xcor(), shift_y * step_size)
        my_turtle.dot()
    my_turtle.forward(step_size)
    if col < size:
        draw_triangle(size, col + 1)


my_turtle = turtle.Turtle()
my_turtle.penup()
step_size = 10
for triangle_size in range(1, 8):
    draw_triangle(triangle_size)
    my_turtle.forward(step_size)

Upvotes: 1

cdlane
cdlane

Reputation: 41872

I like @DenZakh's recursive solution (+1) but it uses recursion to draw the rows of the triangles and iteration to draw the different size triangles. I pictured a solution that instead uses nested loops of iteration to draw a triangle but uses recursion to draw all the increasingly larger triangles in the sequence:

from turtle import Screen, Turtle

STEP_SIZE = 10

def drawTriangles(turtle, dots, steps=1):
    x, y = turtle.position()

    for step in range(steps):
        turtle.goto(x, y + step * STEP_SIZE)
        turtle.dot()

        for _ in range(steps - step - 1):
            turtle.backward(STEP_SIZE)
            turtle.dot()

    if steps < dots:
        turtle.goto(x + STEP_SIZE * (steps + 2), y)
        drawTriangles(turtle, dots, steps + 1)

screen = Screen()

yertle = Turtle()
yertle.hideturtle()
yertle.penup()

drawTriangles(yertle, 6)

screen.exitonclick()

enter image description here

Upvotes: 0

btorralba
btorralba

Reputation: 11

I solved it. In case anyone has this problem in the future, here was the solution:

#Function that draws a triangle with n dots as base
def drawTriangularSeries(myTurtle, n):
    sideLength = 10
    x = 0 - sideLength
    y= 200

    myTurtle.penup()
    myTurtle.goto(x, y)
    for i in range(n+1):
        y = 200
        myTurtle.goto(x, y)
        for j in range(i):
            myTurtle.goto(x, y)
            myTurtle.dot()
            y += sideLength
        myTurtle.forward(sideLength)
        x += sideLength
# Function that draws a series of triangles with dPS as base #
def drawTriangle(myTurtle, dotsPerSide, startX, startY):
    sideLength = 10
    x = startX - sideLength
    y = startY

    myTurtle.penup()
    myTurtle.goto(x, y)
    for i in range(dotsPerSide):
        for j in range(i + 2):
            y = startY
            myTurtle.goto(x, y)
            for k in range(j):
                myTurtle.goto(x, y)
                myTurtle.dot()
                y += sideLength
            myTurtle.forward(sideLength)
            x += sideLength
    myTurtle.hideturtle()

Upvotes: 0

Related Questions