def print_checkerboard(size): for i in range(size): # Create an empty string for each row row_str = "" for j in range(size): # If the sum of the row index (i) and column index (j) is even, use 0 # Otherwise, use 1 (this creates the alternating pattern) if (i + j) % 2 == 0: row_str += "0 " else: row_str += "1 " print(row_str) # Example call for an 8x8 board print_checkerboard(8) Use code with caution. Copied to clipboard Explanation of the Logic
function draw_checkerboard(rows, cols, colorA, colorB, top_left_is_colorA): for r from 0 to rows-1: for c from 0 to cols-1: if (r + c) % 2 == 0: color = colorA if top_left_is_colorA else colorB else: color = colorB if top_left_is_colorA else colorA draw_square_at(r, c, color) 9.1.7 checkerboard v2 answers