Write a Python program using the turtle library. Your turtle should draw a line in red, turn left by 60 degrees, then draw another line in blue. Use variables for the colors.
No hints are provided for this challenge. Try to complete it on your own!
Check Your Work
PY-1.1-BP1-MQ51-S20
Run the tests to check your solution.
Run this command in your terminal:
pytest module-1.1/blueprint-1/quest-51
What you should see:
❌ test_draw_two_colored_lines_behavior
Write Your Code
PY-1.1-BP1-MQ51-S25
Now, let's write your Python code. Remember, you need to use variables for the colors.
Here are some helpful code examples from the Turtle documentation:
Control whether the turtle draws when it moves. By default, the pen is down.
my_turtle.penup(): Lifts the pen. The turtle will move without drawing.
Aliases: my_turtle.pu(), my_turtle.up()
my_turtle.pendown(): Puts the pen down. The turtle will draw when it moves.
Aliases: my_turtle.pd(), my_turtle.down()
my_turtle.penup() # Stop drawing
my_turtle.forward(50) # Move without drawing
my_turtle.pendown() # Start drawing again
my_turtle.forward(50) # Draw a line
7. Appearance
Change the look of the turtle's drawing or the turtle itself.
my_turtle.pencolor(color_name_or_code): Sets the drawing color. Can use color names (e.g., "red", "blue") or hex color codes (e.g., "#FF0000").
my_turtle.pencolor("green")
my_turtle.forward(100) # Draws a green line
my_turtle.speed(speed_value): Sets the turtle's animation speed.
0: Fastest (no animation)
1: Slowest
10: Fast
Values between 1 and 10 increase speed.
my_turtle.speed(1) # Slow speed for observation
my_turtle.speed(0) # Fastest speed
my_turtle.hideturtle(): Makes the turtle icon invisible.
Alias: my_turtle.ht()
my_turtle.showturtle(): Makes the turtle icon visible.
Alias: my_turtle.st()
my_turtle.hideturtle() # Hide the turtle after drawing
8. Positioning
Move the turtle directly to a specific point on the screen using coordinates. The center of the screen is (0, 0).
my_turtle.goto(x, y): Moves the turtle to the point with coordinates (x, y).
my_turtle.penup() # Lift pen before moving
my_turtle.goto(50, 50) # Move to coordinate (50, 50)
my_turtle.pendown() # Put pen down to draw from here
my_turtle.forward(50) # Draw a line from (50, 50)
9. Drawing Shapes
Shapes are drawn by combining movement and turning commands.
Squares: 4 equal sides, 4 x 90-degree turns.
side_length = 100
for _ in range(4):
my_turtle.forward(side_length)
my_turtle.left(90) # Or right(90)
Repeating Actions with Loops (for loop): Execute a block of code multiple times. Useful for drawing shapes with repeating patterns (like squares or triangles) or drawing multiple similar objects.
# Repeat an action 5 times
for i in range(5):
# Code inside the loop runs 5 times
my_turtle.forward(50)
my_turtle.right(60)
# Iterate over items in a list
colors = ["red", "orange", "yellow", "green", "blue"]
for color in colors:
my_turtle.pencolor(color)
my_turtle.forward(20)
Organizing Code with Helper Functions: Break down complex drawing tasks into smaller, reusable functions.
def draw_square(t, size):
"""Draws a square with given size using turtle t."""
for _ in range(4):
t.forward(size)
t.left(90)
# Use the function
artist = turtle.Turtle()
draw_square(artist, 80) # Draw a square of size 80
11. Keeping the Window Open
After your script finishes drawing, the Turtle window will close immediately unless you tell it to wait.
screen.exitonclick(): Waits for you to click on the screen before closing.
turtle.done(): Similar to exitonclick(), often used at the end of a script.
screen.mainloop(): Also keeps the window open; used in event-driven Turtle programs.
# At the end of your script:
screen = turtle.Screen() # Get the screen object if not already in scope
screen.exitonclick()
# OR
# turtle.done()