278. Python Program to Print a Chessboard Pattern
EasyPhase 1 · Warm-up
You need a loop inside a loop, if and the % sign. You have used all three.
Overview
A chessboard has dark and light squares that take turns. This pattern trains nested loops: one loop for the rows, and one for the squares in each row.
Problem Statement
Mateo prints a checkers board on paper, to play on a train. Read a whole number n and print a board of n rows, with n characters in each row:
#is a dark square and.(a full stop) is a light square.- The top-left square is
#. - The signs take turns along each row and down each column.
There are no spaces between the signs.
Steps
- Read
nand turn it into a whole number. - Loop over the rows. Start each row as an empty string.
- In each row, loop over the columns. Count rows and columns from 0. Add
#when the row number plus the column number is even, and.when it is odd. - Print the row when it is complete.
Examples
Example 1. Input 4 gives:
#.#.
.#.#
#.#.
.#.#
Why: row 1 starts with # and row 2 starts with .. The signs take turns in every row and in every column.
Example 2. Input 3 gives:
#.#
.#.
#.#
Why: with an odd size, the first and the last rows both start and end with #.
Input
One whole number n (1 or more). Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
n lines of n characters each, with no spaces. Every line starts at the left edge, with no space in front. The tests try 5 different inputs.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.