Turtle backward(): going back to the start
backward() moves the turtle back the way it came, undoing a forward().
With right() to undo a left(), you can bring the turtle back to its start.
Read Turtle graphics in Python first.
Move back with backward()
t.backward(100) moves the turtle 100 steps the other way. It still faces
the same way, and the pen still draws.
import turtlet = turtle.Turtle()t.forward(100)t.backward(100)print(t.position())It printed (0.00,0.00): the turtle drew a line, then went back over it to
where it started.
Undo a turn with right()
right() turns the other way to left(). Turn left by an angle, then right
by the same angle, and the turtle faces the way it did before.
import turtlet = turtle.Turtle()t.left(30)print(t.heading())t.right(30)print(t.heading())It printed 30.0, then 0.0: the turtle faces right again.
The angle can be a variable. t.right(angle * 2) turns twice as far, so it
undoes the left turn and turns angle past it:
import turtlet = turtle.Turtle()angle = 30t.left(angle)t.right(angle * 2)print(t.heading())t.left(angle)print(t.heading())After the right turn the turtle points 30 to the right of where it started.
A heading never goes below 0, so that prints as 330.0. One more
t.left(angle) brings it back to 0.0.
Out and back with a variable length
A length can be worked out from a variable. size * 0.7 is 70.0 when
size is 100. Use the same sum to go out and to come back:
import turtlet = turtle.Turtle()size = 100angle = 30t.left(angle)t.forward(size * 0.7)t.backward(size * 0.7)t.right(angle)print(t.position())print(t.heading())The turtle draws one short line, 70 long, then comes back down it. The last turn undoes the first, so it ends where it began, facing the same way.
See also: Turtle graphics in Python, Turtle coordinates.
Your turn
Bring the turtle home: add two lines that turn it right by 45, then move it
backward 80.
(0.00,0.00) 0.0
import turtlet = turtle.Turtle()t.forward(80)t.left(45)# add two lines: turn right 45, then move backward 80print(t.position())print(t.heading())