To solve the exercise, you need to create an 8x8 grid (a 2D list) and fill it with alternating 0s and 1s to form a checkerboard pattern.
Instead of two colors, use four colors repeating. Use (row + col) % 4 to choose from an array of colors.
for row in range(rows): for col in range(cols): # Draw square here Use code with caution. 2. Alternating Logic (The Core of V2)
CodeHS exercise 9.1.7 Checkerboard V2 requires students to generate an 8x8 2D list, using nested loops and the modulus operator to create an alternating pattern. The core logic involves evaluating (row + col) % 2 to determine if a cell receives a 0 or 1, a key differentiator from the simpler, row-based V1 assignment. Read user discussions on the solution at Reddit .
What of CodeHS are you using? (Python Tracy, Python Graphics, or JavaScript?) 9.1.7 Checkerboard V2 Codehs
row_1 = [] for i in range(8): # Even index (0, 2, 4, 6) -> 0, Odd index (1, 3, 5, 7) -> 1 if i % 2 == 0: row_1.append(0) else: row_1.append(1) my_grid.append(row_1) # Add the row to the board
If you’re working through the CodeHS Java (or JavaScript) Graphics track, you’ve probably reached . This is a classic “level-up” from the basic checkerboard challenge. It’s designed to test your understanding of nested loops, conditional logic, and coordinate math.
Do your specific CodeHS autograder instructions require (like "black" / "white" ) or integers ( 0 / 1 )?
: This is the most efficient way to determine if a row or column index is even or odd. For a checkerboard, a cell (row, col) usually contains a 1 if the sum of its indices (row + col) is even, and a 0 otherwise. To solve the exercise, you need to create
The iterates through the columns of the current row. 3. Apply the Modulo Condition
Here is the correct Python code using the Turtle module to solve this problem.
Some versions of this exercise require you to start with a board of all s and use a nested loop to set specific indices to . In this case: Iterate through each row and each column (i + j) % 2 != 0 board[i][j] = 1 Example Python Solution
You can now combine this logic into a single, powerful line of code using a list comprehension, which will build the full board in one concise expression: for row in range(rows): for col in range(cols):
This approach ensures that adjacent squares have different colors, resulting in the characteristic checkerboard pattern.
A 2D array is essentially an array of arrays. In Java or JavaScript (the primary languages used in CodeHS), the first index typically represents the row , and the second index represents the column .
) is even or odd. If the sum is even, use Color A; if odd, use Color B. Step-by-Step Implementation 1. Define Constants
Using the % (remainder) operator to determine when a square should be color A versus color B.