28
E X E R C I S E # 9 : C H E S S S Q U A R E C O L O R
getChessSquareColor(8, 8)
→
'white'
getChessSquareColor(2, 1)
→
'black'
A chess board has a checker-pattern of white and black tiles. In this program, you’ll
determine a
pattern to the color of the squares based on their column and row. This program challenges you to
take a real-world object
such as a chess board, determine the patterns behind its design, and translate
that into Python code.
Exercise Description
Write a getChessSquareColor() function that has parameters column and row. The
function either returns 'black' or 'white' depending on the color at the specified column and
row
. Chess boards are 8 x 8 spaces in size, and the columns and rows in this program begin at 0 and
end at 7 like in Figure 9-1. If the arguments for column or row are outside the 0 to 7 range, the
function returns a blank string.
Note that chess boards always have a white square in the top left corner.
Figure 9-1: A chess board with labeled columns and rows.
These Python assert statements stop the program if their condition is False.
Copy them to
Python Programming Exercises,
Gently Explained
29
the bottom of your solution program. Your solution is correct if the following assert statements’
conditions are all True:
assert getChessSquareColor(1, 1) == 'white'
assert getChessSquareColor(2, 1) == 'black'
assert getChessSquareColor(1, 2) == 'black'
assert getChessSquareColor(8, 8) == 'white'
assert getChessSquareColor(0, 8) == ''
assert getChessSquareColor(2, 9) == ''
Try to write a solution based on the information in this description. If you still have trouble
solving
this exercise, read the
Solution Design and
Special Cases and Gotchas sections for
additional hints.
Prerequisite concepts: Boolean operators,
modulo operator
Dostları ilə paylaş: