Hi all.
I have an up and coming midterm, and this is one of the practice problems. All's I need to do is draw a Tic-Tac-Toe board in TURTLE. However, it has to be in a loop, or something, and I am having a difficult time putting it into one. Here is my code:


from turtle import *

up()
goto(50,50)
right(90)
down()
forward(100)

up()
goto(-50,50)
down()
forward(100)

up()
goto(-100,25)
left(90)
down()
forward(200)

up()
goto(-100,-25)
down()
forward(200)

seemed to have solved it using this:


from turtle import *

right(90)
for i in range(2):
up()
goto(i*100,50)
down()
forward(100)
up()

left(90)
for i in range(1,3):
up()
goto(-50,i*-50+75)
down()
forward(200)
up()

Do not use absolute goto unnecessary, your code is not according to idea of turtle graphics. You could check my histogram code from code snippets for example of loop with turtle graphics.

Okay thanks. This is how my professor has been teaching us so far. He likes leaving bits and pieces out of lectures and what not.

Looks more understandable with [CODE] tags

from turtle import *

right(90)
for i in range(2):
    up()
    goto(i*100,50)
    down()
    forward(100)
    up()

left(90)
for i in range(1,3):
    up()
    goto(-50,i*-50+75)
    down()
    forward(200)

The last up inside first loop does not make so much sense, better at least remove that. So you wanted to make crossing grid of two parallel lines

You can see what I meant if you change the orientation before you routine, say putting right(234) as original orientation of turtle.

commented: Thanks for your more clear in-depth explanation +1

Here I would not so much use loops but functions:

from turtle import *

def two_parallel(length, separation):
    down()
    forward(length)
    up()
    right(90)
    forward(separation)
    right(90)
    # facing back
    down()
    forward(length)
    right(90)
    up()
    forward(separation)
    right(90)


def crossing(length, separation, pattern):
    pattern(length, separation)
    up()
    forward(length/2+separation/2)
    right(90)
    up()
    back(length/2-separation/2)
    pattern(length, separation)

# demonstrate arbitrary orientation
right(234)
crossing(200, 80, two_parallel)
mainloop()

Thanks everyone for your help, much appreciated :)!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.