ChatGPT Technical Tutor
I’ve been slowly figuring out the best way to approach learning with ChatGPT that’s not just give code, get back fixed code.
While that no doubt is quick and efficient, it does little toward helping me progress my own knowledge base. However, finding a way to get back sourced information–that I don’t have to skim the documentation for myself–is a win!
Over the past week, I’ve noticed a massive improvement in the quality of feedback. Not sure if it’s model upgrades on the server side, better prompting on my part, or a mixture of both.
This is by far the best result I’ve gotten yet!
Prompting
Priming Question: Are you familiar with the turtle module for python?
Response:
Yes! The turtle
module in Python is a standard library module used for creating simple graphics and animations. It provides a way to control a turtle (a graphical pen) that moves around the screen, drawing shapes, lines, and patterns. It’s often used for teaching programming concepts, especially in an interactive and visual way.
Would you like help with a specific turtle
project or issue?
Core Question:
Current code:
import turtle as T
screen = T.Screen()
timmy = T.Turtle()
def move_forward():
global timmy
timmy.forward(10)
def move_backward():
global timmy
timmy.backward(10)
def clockwise():
global timmy
timmy.right(36)
def counter_clockwise():
global timmy
timmy.left(36)
def clear_screen():
global screen
screen.clear()
main()
def main():
global timmy, screen
screen.listen()
screen.onkey(key="w", fun=move_forward)
screen.onkey(key="s", fun=move_backward)
screen.onkey(key="d", fun=clockwise)
screen.onkey(key="a", fun=counter_clockwise)
screen.onkey(key="c", fun=clear_screen)
screen.exitonclick()
main()
my clear_screen()
doesn’t restart with the arrow in the center of the screen. How should I go about fixing this? Please used detailed explanations and site source documentation.
Takeaways:
- Using a priming question to load relevant information.
- Provide context (whole code snippet).
- Explain your concern/issue
my clear_screen() doesn't restart with the arrow in the center of the screen.
- Ask for guidance, not answer
How should I go about fixing this?
Please used detailed explanations and site source documentation.
- Reiterate explanation for learning purposes.
Site Sources
will keep it focused on the original priming material.
Excellent Answer!
Your issue arises because calling screen.clear()
only clears the screen—it does not reset the turtle’s position, orientation, or reinitialize the Turtle
instance. Additionally, calling main()
after clear_screen()
does not recreate the Turtle
object, which means timmy
is lost when screen.clear()
is called.
Why Doesn’t clear_screen()
Work as Expected?
screen.clear()
removes everything, including theTurtle
object.- The documentation for
clear()
states that it clears the drawings but does not reset the turtle’s position or heading. - However,
screen.clear()
clears everything from the screen, including all turtles.
- The documentation for
The
timmy
object is lost afterscreen.clear()
.- Since
timmy
is created at the start ofmain()
, it needs to be reinitialized after clearing the screen.
- Since
The event listeners (
onkey
) may be lost after clearing the screen.screen.clear()
removes event listeners, somain()
needs to reassign them.
Solution: Reset the Turtle After Clearing
Instead of calling screen.clear()
, you can:
- Reset the turtle’s position manually.
- Reset its heading (orientation).
- Clear only the drawings (
timmy.clear()
) instead of clearing the entire screen.
Fixed Code:
import turtle as T
screen = T.Screen()
timmy = T.Turtle()
def move_forward():
timmy.forward(10)
def move_backward():
timmy.backward(10)
def clockwise():
timmy.right(36)
def counter_clockwise():
timmy.left(36)
def clear_screen():
"""Clears the screen and resets the turtle to the center."""
timmy.clear() # Clears the drawing
timmy.penup() # Lifts the pen to avoid drawing while moving
timmy.home() # Moves turtle to (0,0) and sets heading to default (East)
timmy.pendown() # Puts the pen down to resume drawing
def main():
"""Sets up the screen and key bindings."""
screen.listen()
screen.onkey(key="w", fun=move_forward)
screen.onkey(key="s", fun=move_backward)
screen.onkey(key="d", fun=clockwise)
screen.onkey(key="a", fun=counter_clockwise)
screen.onkey(key="c", fun=clear_screen)
screen.exitonclick()
main()
Explanation of Fixes
Using
timmy.clear()
Instead ofscreen.clear()
screen.clear()
removes everything, buttimmy.clear()
only clears the turtle’s drawings while keeping it active.clear()
documentation states: “Delete the turtle’s drawings from the screen. Do not move turtle.”
Using
timmy.home()
Instead of Recreating the Turtlehome()
moves the turtle to the center (0,0
) and resets its heading to the default (east).home()
documentation states: “Move turtle to the origin – coordinates (0,0) – and set its heading to its start orientation (east).”
Using
timmy.penup()
andtimmy.pendown()
to Avoid Unwanted Drawingpenup()
prevents the turtle from drawing while repositioning.pendown()
resumes drawing once it’s reset.penup()
documentation states: “Pull the pen up – no drawing when moving.”
Alternative: If You Want a Full Reset
If you truly need to reset the entire screen, you must recreate the turtle after clearing it:
def clear_screen():
"""Fully resets the screen, including event bindings."""
global timmy
screen.reset() # Resets everything (screen, turtles, events)
timmy = T.Turtle() # Recreate the turtle
timmy.speed("normal") # Optional: Set turtle speed
main() # Rebind keys
screen.reset()
clears the screen and reinitializes everything, but you must recreate the turtle and event bindings afterward.
Which Solution Should You Use?
- Use
timmy.clear()
andtimmy.home()
if you only want to clear the drawing and reset the turtle. - Use
screen.reset()
if you want to completely reset the environment.
Would you like to customize the behavior further? 😊