I have started learning Python and playing around with the Turtle drawing various shapes. I decided to draw n nested squares and have achieved the following result:
However as you can see, after the 1st iteration the position of the second square has been shifted too much to the left/down direction. And all the following squares look ok. I expect all the squares to have the same offset from each other.
How should I modify my code to fix this?
def draw_squares(side_len, num_squares, side_increment): for i in range(num_squares): draw_polygon(4, side_len) pen.up() pen.setposition(x - side_len/2, y - side_len/2) pen.down() side_len += side_increment side_length = 50 num_of_squares = 10 side_incr = 40 draw_squares(side_length, num_of_squares, side_incr) turtle.done() 1 Answer
I think your problem is that the first square is starting at (0,0). You can easily correct this problem by setting the position before calling the draw_polygon() function.
def draw_squares(side_len, num_squares, side_increment): for i in range(num_squares): pen.up() pen.setposition(x - side_len/2, y - side_len/2) pen.down() draw_polygon(4, side_len) side_len += side_increment If this doesn't solve the problem pls share your draw_polygon() function.
