Add draw_square and draw_dashed_line with tests

Challenge 1 draws four sides and turns right 90 degrees after each. Challenge 2 puts
the pen down for a dash and up for a gap and leaves it down at the end. Lengths and
counts come from constants.py; invalid values raise ValueError.

Task: MIL-002#3
Task: MIL-002#4
Task: MIL-002#5
This commit is contained in:
2026-10-07 23:58:52 +08:00
parent e2249de46e
commit a34bf6bbc2
6 changed files with 217 additions and 1 deletions
+26
View File
@@ -0,0 +1,26 @@
"""!
@file square.py
@brief Turtle Challenge 1: draw a square.
"""
from turtle_challenges.constants import (
RIGHT_ANGLE_DEGREES,
SQUARE_SIDE_LENGTH,
SQUARE_SIDES,
)
from turtle_challenges.pen import Pen
def draw_square(pen: Pen, side_length: float = SQUARE_SIDE_LENGTH) -> None:
"""!
@brief Draw a square: move forward, turn right, four times.
@param pen The turtle that draws.
@param side_length Length of each side, in turtle units.
@exception ValueError If @p side_length is not positive.
"""
if side_length <= 0:
raise ValueError(f"side_length must be positive, got {side_length}")
for _ in range(SQUARE_SIDES):
pen.forward(side_length)
pen.right(RIGHT_ANGLE_DEGREES)