Codehs - 9.1.7 Checkerboard V2
Nested Loops
This problem is a classic introduction to and Modular Arithmetic . It asks you to draw a checkerboard pattern where the color of each square depends on its position (row and column).
Understanding the Problem
Goal:
Draw a checkerboard pattern using alternating colored squares (usually black and red or black and white). The board should have 8 rows and 8 columns. However, V2 typically introduces two key requirements: 9.1.7 Checkerboard V2 Codehs
- Each row has 8 columns.
isBlackis toggled 8 times per row.- 8 is even, so after the inner loop,
isBlackreturns to the same value it had at the start of that row. - But the next row should start with the opposite color.
- Therefore, after each row, we must manually toggle
isBlackonce more ifCOLSis even.
Mistake #2: Off-by-One Errors
- Canvas size: 400x400
- Square size: 50x50 (since 8 * 50 = 400)
# This is the logic for the checkerboard pattern: # If the sum of the row and column indices is even, make it red. # Otherwise, make it black. if (row + col) % 2 == 0: pen.color("red") else: pen.color("black") Nested Loops This problem is a classic introduction
pen.speed(0): This is highly recommended. Drawing 64 squares one by one takes a long time at normal speed. Setting speed to 0 makes the turtle draw instantly, which is crucial for testing your code quickly in the CodeHS editor.
System.out.println(); // new line after each row Each row has 8 columns