← Back to Nested Lists
♟️

Checkerboard

5 of 6

Exercise 4: Checkerboard

Create and print a 4×4 checkerboard pattern:

B W B W
W B W B
B W B W
W B W B

Starter Code

for r in range(4): for c in range(4): if ___: print("B", end=" ") else: print("W", end=" ") print()

Hints

Hint 1 — Look at the pattern

Notice that B appears when (row + col) is even, and W when it's odd:

  • (0,0) = 0+0 = 0 (even) → B
  • (0,1) = 0+1 = 1 (odd) → W
  • (1,0) = 1+0 = 1 (odd) → W
  • (1,1) = 1+1 = 2 (even) → B
Hint 2 — How to check even/odd?

Use the modulo operator %. A number is even when number % 2 == 0.

if (r + c) % 2 == 0:

Bonus Challenge

Make it any size! Ask the user for the number of rows and columns:

rows = int(input("Rows: ")) cols = int(input("Cols: ")) # Print the checkerboard

Solution

Show Solution
for r in range(4): for c in range(4): if (r + c) % 2 == 0: print("B", end=" ") else: print("W", end=" ") print()

How it works:

  • For each position, add the row and column numbers
  • If the sum is even → print B
  • If the sum is odd → print W
  • The % 2 == 0 check tells us if a number is even
Show Bonus Solution
rows = int(input("Rows: ")) cols = int(input("Cols: ")) for r in range(rows): for c in range(cols): if (r + c) % 2 == 0: print("B", end=" ") else: print("W", end=" ") print()
💻

Try it yourself

Code: Checkerboard

Loading Python runtime…
Python
Loading...
Output
Click "Run" to execute your code...
ℹ️ About this Python environment

✅ Standard library: heapq, collections, itertools, math, random, functools, datetime, bisect

✅ Functions, classes, recursion, print()

❌ No file system, subprocess, OS access, or network requests

❌ No pip install (all supported modules are pre-loaded)

⏱️ 5 second execution time limit