Tel: 0800 1583746 Email: info@cscservices.uk
Family run business
Pseudocode approach:
set square_size = 50 set rows = 8 set cols = 8
for each row in range(rows): for each col in range(cols): x = col * square_size y = row * square_size if (row + col) % 2 == 0: set fill color to red else: set fill color to black draw square at (x, y) with size square_size
Key line:
if (row + col) % 2 == 0 — this creates the perfect alternating pattern.
After implementing the code above, run the program. You should see:
If the board starts with black instead of red, simply swap the colors in the if-else block.
This is the mathematical trick to the checkerboard pattern.
You’re usually asked to create a checkerboard pattern (alternating black and red squares) using a graphics library (like graphics.py or JavaScript’s Graphics in CodeHS).
Example structure (not exact solution):
var SQUARE_SIZE = 50;
for(var row = 0; row < 8; row++)
for(var col = 0; col < 8; col++)
var x = col * SQUARE_SIZE;
var y = row * SQUARE_SIZE;
var color = (row + col) % 2 === 0 ? "red" : "black";
var rect = new Rectangle(SQUARE_SIZE, SQUARE_SIZE);
rect.setPosition(x, y);
rect.setColor(color);
add(rect);
Running this code will produce a standard 8x8 checkerboard pattern with alternating black and white squares. 916 checkerboard v1 codehs fixed
Step-by-Step Solution:
To fix the CodeHS 9.1.6: Checkerboard, v1 assignment, you must ensure you are not just printing the final output, but actually modifying the elements of a 2D list (grid) using assignment statements. The autograder specifically checks for code that sets elements to 1. Fixed Python Code
# Create an 8x8 board filled with 0s board = [] for i in range(8): board.append([0] * 8) # Modify the board to set the top 3 rows and bottom 3 rows to 1s # Middle 2 rows (index 3 and 4) remain 0s for i in range(8): for j in range(8): if i < 3 or i > 4: board[i][j] = 1 # Function to print the board def print_board(board): for row in board: # Convert each integer to a string and join with spaces print(" ".join([str(x) for x in row])) print_board(board) Use code with caution. Copied to clipboard 1. Initialize the 2D List
Start by creating a grid of 8 lists, each containing 8 zeros. This establishes the base "empty" board required by the exercise. 2. Use Nested Loops for Assignment
The autograder requires you to use an assignment statement (e.g., board[i][j] = 1). You should loop through the rows and columns, checking if the row index i is in the top three (0, 1, 2) or the bottom three (5, 6, 7). 3. Implement the Print Function
Define a function that iterates through each row of your 2D list. Use a list comprehension to convert the integers to strings so they can be joined by spaces for the final display. 4. Avoid Common Errors
Inner Definitions: Do not define your print_board function inside another function or loop; it should be at the top level of your script.
Simple Printing: Do not just use print("1 1 1...") manually. The assignment tests your ability to access and replace values within a list. ✅ Final Result
The code successfully creates an 8x8 grid where the top three and bottom three rows are filled with 1s, while the middle two rows remain 0s, satisfying both the visual output and the autograder's logic requirements. Pseudocode approach: set square_size = 50 set rows
function start()
const SIZE = 50;
for(let row = 0; row < 8; row++)
for(let col = 0; col < 8; col++)
let x = col * SIZE;
let y = row * SIZE;
let color = (row + col) % 2 === 0 ? "red" : "black";
let rect = new Rectangle(SIZE, SIZE);
rect.setPosition(x, y);
rect.setColor(color);
add(rect);
import turtledef draw_square(color): turtle.color(color) turtle.begin_fill() for _ in range(4): turtle.forward(50) turtle.left(90) turtle.end_fill() turtle.forward(50)
def next_row(): turtle.penup() turtle.backward(400) turtle.right(90) turtle.forward(50) turtle.left(90) turtle.pendown()
def main(): turtle.speed(0) for row in range(8): for col in range(8): if (row + col) % 2 == 0: draw_square("red") else: draw_square("black") next_row() turtle.hideturtle() turtle.done()
main()
If you paste your specific broken code or the exact error message from CodeHS, I can give you the exact line-by-line fix. Would you like that?
The core objective of CodeHS 9.1.6: Checkerboard, v1 is to practice modifying 2D lists using nested loops and index-based assignment.
The autograder often fails students who simply print the pattern; it strictly requires that you initialize a board of 0s and then use assignment statements (e.g., board[i][j] = 1) to place pieces. Fixed Python Solution
This code initializes an 8x8 grid of zeros and then fills the top three and bottom three rows with a checkerboard pattern of 1s. Key line: if (row + col) % 2
# Function to print the board def print_board(board): for row in board: # Join elements with a space for proper formatting print(" ".join([str(x) for x in row])) # 1. Initialize an 8x8 board with all zeros board = [] for i in range(8): board.append([0] * 8) # 2. Use nested loops to assign 1s to specific indices # Row indices 0, 1, 2 are the top three rows # Row indices 5, 6, 7 are the bottom three rows for row in range(8): for col in range(8): # Check if the row should have pieces if row < 3 or row > 4: # Only set to 1 if (row + col) is even to create the pattern if (row + col) % 2 == 0: board[row][col] = 1 # 3. Display the final board print_board(board) Use code with caution. Copied to clipboard Key Logic & Fixes 💡
Explicit Assignment: You must use board[row][col] = 1. The autograder specifically looks for the = assignment operator being used on the list elements.
The Modulo Trick: Using (row + col) % 2 == 0 ensures that the 1s alternate correctly across both rows and columns.
Row Filtering: The condition if row < 3 or row > 4 targets the "top 3" (0, 1, 2) and "bottom 3" (5, 6, 7) rows as required by the exercise.
String Formatting: Using " ".join(...) ensures the output matches the expected CodeHS console format exactly, preventing "Output does not match" errors. Common Errors to Avoid
Defining print_board inside another function: This leads to scope errors where the autograder can't find your display logic.
Using hardcoded print statements: Printing 1 0 1 0... directly will pass the visual check but fail the "You should set some elements of your board to 1" test.
If you're also working on 9.1.7: Checkerboard, v2, let me know—the logic changes slightly to focus on a full-board pattern or different row offsets.
This report includes the Problem Statement, Algorithm Analysis, the Corrected Code Solution, and a detailed Code Breakdown to ensure the "fixed" requirements are met (specifically addressing the common issue where the code runs infinitely or crashes due to missing decrement logic).