SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
Functions Strings And Objects
Exercise:
Check Point
Question:29 | ISBN:978013274719 | Edition: 6

Question

How do you undo the turtle’s last action?

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

To undo the turtle's last action in Python, you can use the turtle.undo() function. This function reverts the turtle's state back to the previous state, effectively undoing the last action performed.

Take an example to use the turtle.undo() function:

import turtle

# Create a turtle object
t = turtle.Turtle()

# Move the turtle forward
t.forward(100)

# Undo the last action
t.undo()

# Move the turtle backward
t.backward(50)

# Undo the last action again
t.undo()

# Close the turtle graphics window
turtle.done()

Exaplanation:

  • Here,  first import the turtle module and create a turtle object called t.
  • We then move the turtle forward by calling the forward() method with a distance of 100. After that, we use the undo() function to undo the last action, which was moving forward. This brings the turtle back to its initial position.
  • Next, we move the turtle backward by calling the backward() method with a distance of 50. Again, we use the undo() function to undo the last action, which was moving backward. This brings the turtle back to its initial position once more.
  • Finally, we close the turtle graphics window using the turtle.done() function.
  • By using the turtle.undo() function, you can easily undo the turtle's last action and revert its state to a previous point in the drawing.
0 0

Discussions

Post the discussion to improve the above solution.