This is what I've tried so far. I don't know how to center the squares so they are not getting one over the other.
import turtle
def square(t, sz):
for i in range(4):
t.color("hotpink")
t.forward(sz)
t.left(90)
wn = turtle.Screen()
wn.bgcolor("lightgreen")
alex = turtle.Turtle()
alex.pensize(3)
size = 10
for _ in range(5):
square(alex, size)
alex.penup()
alex.right(90)
alex.left(90)
alex.pendown()
size += 20
Ok, this is an interesting problem to make you think how to solve it.
The code you have starts each square from the bottom left corner.
Sure, you could think what correction you need to apply at each iteration, based on the square size.
But there's a better way, encapsulate everything inside square.
If you want squares to be concentric, modify the square function so it assumes that the start position is the center and the end position is the center.
Then it will be as easy as just calling
t.squareinside the loop., since the square itself handles the start and end position.Basically we will first need to move from the center to a corner with the pen up, then draw the 4 sides and finally go back to the center.
This is a nice example on how something that could seem hard at the beginning can be solve in an easy way if you make each iteration start with known conditions and take care to leave the system in the same conditions it found it.