livepython.net

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

  1. Read n and turn it into a whole number.
  2. Loop over the rows. Start each row as an empty string.
  3. 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.
  4. 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.

Finish this chapter firstChapter 10 · For Loops
solution.py
# write your code here

EXPECTED OUTPUT

1#.#.
2.#.#
3#.#.
4.#.#

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.