siegel siegel - 1 month ago 11
Python Question

Concentric Squares in Python with Turtle

Trying to make concentric squares in python with Turtle. Here's my attempt:

import turtle
def draw_square(t, size):
for i in range(4):
t.forward(size)
t.left(90)
wn = turtle.Screen()
dan = turtle.Turtle()

sizevar = 1
for i in range(10):
draw_square(dan,sizevar)
sizevar += 20
dan.penup()
dan.backward(sizevar/ 2)
dan.right(90)
dan.forward(sizevar / 2)
dan.left(90)
dan.pendown()


I'm not sure why they aren't concentric, my
dan.backward(sizevar/2)
and
dan.forward(sizevar/2)
lines seem to move the square down and to the left too much?

Answer

It should be like this:

import turtle
def draw_square(t, size):
   for i in range(4):
       t.forward(size)
       t.left(90)
wn = turtle.Screen()
dan = turtle.Turtle()
sizevar = 1
for i in range(10):
   draw_square(dan, sizevar)
   sizevar += 20
   dan.penup()
   dan.backward(10)
   dan.right(90)
   dan.forward(10)
   dan.left(90)
   dan.pendown()

Each square is only 10 pixels outside the previous.

Comments