9.1.7 Checkerboard V2 Codehs -
This article provides a comprehensive walkthrough for completing the exercise in CodeHS. This challenge builds upon basic looping concepts by introducing nested loops and conditional logic to create a complex visual pattern. Understanding the Objective
# 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")
"Pass this function a list of lists and print it such that it looks like the grid in the exercise instructions."
grid of alternating values (typically 0 and 1) to represent a checkerboard pattern. 9.1.7 Checkerboard V2 Codehs
Let’s assume the following constants (typical in CodeHS):
def print_board(board): for row in board: print(" ".join([str(x) for x in row])) print_board(board) Use code with caution. Copied to clipboard ✅ Result The final output will be an
Remember: x = col * size , y = row * size . The column determines horizontal position (x), the row determines vertical position (y). # Otherwise, make it black
var rect = new Rectangle(x, y, SQUARE_SIZE, SQUARE_SIZE); rect.setColor(color); rect.setFilled(true); add(rect);
function start() for (var row = 0; row < NUM_ROWS; row++) for (var col = 0; col < NUM_COLS; col++) // Calculate coordinates var xPos = col * SQUARE_SIZE; var yPos = row * SQUARE_SIZE; // Determine alternating color if ((row + col) % 2 === 0) drawSquare(xPos, yPos, Color.RED); else drawSquare(xPos, yPos, Color.BLACK); Use code with caution. Common Mistakes and How to Avoid Them
Instead of 0s and 1s, you can customize the board by using any two characters or strings to represent the pattern. For instance, using "X" and "O" works just as well. The alternating logic ( (i + j) % 2 ) remains exactly the same. Copied to clipboard ✅ Result The final output
Happy coding! 🧩
Ensure you are multiplying by SQUARE_SIZE and not adding it. Multiplication ensures perfect mathematical alignment on the canvas grid without leaving 1-pixel white lines between blocks. If you want to customize your board further, let me know:
# # # # # # # # # ...