Untitled

 avatar
unknown
plain_text
a year ago
4.0 kB
10
Indexable
import turtle

# Function 1: draw a square of given size
def draw_square(size):
    for _ in range(4):
            turtle.forward(size)
                    turtle.right(90)

                    # Function 2: draw nested squares and cross, with turtle ending in center
                    def nested_squares(x):
                        size = 40 * x   # starting size depends on x
                            step = 20       # shrink step

                                # Draw squares
                                    for i in range(x):
                                            draw_square(size)
                                                    turtle.penup()
                                                            turtle.forward(step)
                                                                    turtle.right(90)
                                                                            turtle.forward(step)
                                                                                    turtle.left(90)
                                                                                            turtle.pendown()
                                                                                                    size -= 2 * step

                                                                                                        # Draw cross
                                                                                                            turtle.penup()
                                                                                                                turtle.goto(-step * x, 0)   # left line start
                                                                                                                    turtle.pendown()
                                                                                                                        turtle.forward(step * 2 * x)  # horizontal line

                                                                                                                            turtle.penup()
                                                                                                                                turtle.goto(0, step * x)   # top line start
                                                                                                                                    turtle.setheading(270)     # face down
                                                                                                                                        turtle.pendown()
                                                                                                                                            turtle.forward(step * 2 * x)  # vertical line

                                                                                                                                                # End with arrow in center
                                                                                                                                                    turtle.penup()
                                                                                                                                                        turtle.goto(0, 0)
                                                                                                                                                            turtle.setheading(270)  # arrow facing down

                                                                                                                                                            # Example
                                                                                                                                                            nested_squares(5)
                                                                                                                                                            turtle.done()
Editor is loading...
Leave a Comment