Question
3 views
Python Coding (on PyCharm Community Edition)
WHAT COLOUR SQUARE
PART 1 - Positions on a chess board are identified by a letter and number combination.  See diagram. The letter refers to the column and the number refers to the row :

** MUST USE IF / ELIF / ELSE or some version of it for full marks.  Do not use multiple IF statements unless you are nesting ***

Write a program that reads the position (row and column) and tells the user which colour it will be.  Do this twice.
For example, if the user enters g3 - it should tell you BLACK.  You can use the modulus for this question or lists - we have done similar work in this course.  Create a mathematical formula to work out the position and colour.
* The user should be able to input EITHER upper or lowercase.
check_circle

Step 1

The approach and code of a python program to check whether the position of the square of a chessboard is black or white are given below:

Approach:

• Using a for loop to restrict the user to check the position of a square in the chessboard twice.
• Inside the for loop, while loop is used to check for the condition is true.
• Inside the while loop, if statement will check the input value is a combination of an alphabet and a number. Also, it will check that the alphabet does not exceed to h, and the number is equal to 8.
• In the ADD variable, the sum of ASCII value of the character and number is saved. Now, the ADD variable will be checked in the if-else statement.
• Then if-else statement will check and return that the input value is positioning a white or black square in the chessboard.
Step 2

Code:

#for loop is used to check the position of a square in the chessboard twice

for i in range (0,2):

#displaying message to prompt user to enter the value he wants to check

STR = input("Please enter the value you want to chhess: ")

while True:

#if-else statement is used to check the entered value is in the scope or not

if (STR[0].lower() > "h" or int(STR[1]) > 8 or len(STR) != 2 or int(STR[1]) == 0):

#displaying message to prompt user to enter the correct value

STR = input("Please enter the correct va...

### Want to see the full answer?

See Solution

#### Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in
EngineeringComputer Science