while (frontIsClear()) move(); if (startWithBeeper) // Alternate pattern: after moving, we want opposite // Better approach: move, then if column index is even/odd // But simpler: use a counter
The outer loop ( row ) tells the program to start at the top and move down. For every single row, the inner loop ( col ) runs across from left to right. This ensures every single coordinate on the grid is visited. 2. The Modulo Operator (%) The line (row + col) % 2 == 0 is the "brain" of the code. % 2 finds the remainder when divided by 2. If the remainder is 0 , the number is even. 9.1.6 checkerboard v1 codehs
# Pass this function a list of lists to print it as a grid def print_board ( board ): for i in range(len(board)): print( " " .join([str(x) for x in board[i]])) # 1. Start with an empty board and fill it with 0s board = [] for i in range( 8 ): board.append([ 0 ] * 8 ) # 2. Use nested loops to change 0s to 1s in the correct rows for i in range( 8 ): for j in range( 8 ): # Top 3 rows (0, 1, 2) and bottom 3 rows (5, 6, 7) if i < 3 or i > 4 : board[i][j] = 1 # 3. Print the final result print_board(board) Use code with caution. Copied to clipboard If the remainder is 0 , the number is even