Python Turtle Assignment: Challenge 1 – Getting Started
Welcome to your first Python Turtle challenge! Follow the steps below to create your first Turtle drawing. Use the sidebar to jump to any step or click "Start Challenge" to begin from Step 1.
Step 1: Import the Turtle Module
We need to import the turtle module to start using it in our code.
This line should be added at the top of your Python script:
# Add the following import statement at the top of your code
import turtle
Action: In your code editor, type import turtle at the top of your script.
Step 2: Create a Turtle Object
Now we need a screen for the turtle to draw on, and then create the turtle (our "pen").
# Add these lines after your import statement
# This creates a screen and a turtle named "pen"
screen = turtle.Screen()
pen = turtle.Turtle()
Action: Add these lines to create your screen and turtle.
Step 3: Move the Turtle Forward
To draw, we move the turtle forward a certain distance. Let's move it forward by 100 units.
# Add this line to move the turtle forward 100 units
pen.forward(100)
Action: Add pen.forward(100) to move the turtle forward.
Step 4: Turn the Turtle Right
To eventually form a square, we need to turn the turtle right by 90 degrees after drawing each side.
# Add this line to turn the turtle right by 90 degrees
pen.right(90)
Action: Add pen.right(90) to rotate the turtle 90 degrees.
Step 5: Repeat Moving and Turning
Repeat the forward and turn steps three more times to complete the square.
# Add these lines to draw all four sides of the square
# First side (already done): pen.forward(100) then pen.right(90)
# Second side
pen.forward(100)
pen.right(90)
# Third side
pen.forward(100)
pen.right(90)
# Fourth side
pen.forward(100)
pen.right(90)
Action: Add these lines to complete the square.
Step 6: Simplify with a Loop
Instead of writing the forward and right steps four times, use a loop to make the code cleaner:
# Replace the repeated steps with this loop
for _ in range(4):
pen.forward(100)
pen.right(90)
Action: Use the loop to replace the repetitive code.
Step 7: Keep the Window Open
To view your drawing, keep the window open until you click it by using screen.exitonclick():
# Add this line at the end of your code to keep the window open
screen.exitonclick()
Action: Add screen.exitonclick() at the end of your code.
Complete Code
Here’s the full code for Challenge 1:
# Full Code: Copy and paste into a file and run
import turtle
# Set up the screen
screen = turtle.Screen()
screen.title("Challenge 1: Draw a Square")
screen.bgcolor("lightyellow") # Optional: Change background color
# Create a turtle named "pen"
pen = turtle.Turtle()
pen.color("blue") # Set the pen color
pen.pensize(3) # Set the pen size
# Draw a square using a loop
for _ in range(4):
pen.forward(100) # Move forward by 100 units
pen.right(90) # Turn right by 90 degrees
# Finish drawing and keep the window open until clicked
screen.exitonclick()
Enhancements (Optional)
Try experimenting with:
- Change Pen Color:
pen.color("green") - Change Pen Size:
pen.pensize(5) - Change Background Color:
screen.bgcolor("lightblue") - Change Turtle Speed:
pen.speed(2)
Expected Outcome
When you run the program, a window titled "Challenge 1: Draw a Square" will appear with a blue square on a light yellow background. The window remains open until you click on it.