Turtle Graphics: Repeating Squares

ChrisCrad

I am trying to create a loop that takes an input by a user and draws however many squares but it increases the size of the squares with each loop, however 2 sides are stay connected. I'll include the graphic to better explain.

enter image description here

    import turtle

squares = 1
while squares >= 1:
    squares = int(input('How many squares would you like drawn?:'))
    if squares == 0:
        print("You must have at-least 1 square.")
        squares = int(input('How many squares would you like drawn?:'))
    else:
        for count in range(squares):
            turtle.forward(30)
            turtle.left(90)
            turtle.forward(30)
            turtle.left(90)
            turtle.forward(30)
            turtle.left(90)
            turtle.forward(30)
            turtle.left(90)


turtle.done()
Reblochon Masque

The input request and the drawing logic ought to be separated.
Here is one approach that returns the turtle at the start at each turn, after increasing the side length.

import turtle

num_squares = 3
t = turtle.Turtle()
t.pendown()
side = side_unit = 30

while True:
    try:
        num_squares = int(input('input the number of squares'))
    except ValueError:
        print("please enter an integer")
    if num_squares > 3:
        break

for sq in range(1, num_squares + 1):
    t.left(90)
    t.forward(side)
    t.left(90)
    t.forward(side)
    t.left(90)
    t.forward(side)
    t.left(90)
    side = side_unit + 3 * sq  # increase the size of the side

    t.goto(0,0)                # return to base

turtle.done()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related