Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+5 votes

My code works, but I was wondering if there was a better way to write it.
Right now I have everything in the if/elif statement like

>>> if number == 1 or number == 2 or number == 3 or... and letter == 'a' or letter == 'b' or ...

and then the same with the letter lines, but it's really long.
I tried putting it in a variable but it only registered the number 1 and the string 'a' when I did it that way...

any suggestions on how to make my if/elif statements shorter?

asked in CSC201 Spring 2021 by (1 point)

2 Answers

+6 votes

I use only three if/elif/else for this question.

Specifically, the first statement is to determine if the parameter is equal to 'a1' or 'a8' or 'h1' or 'h8'. This first if will help you return the 'Corner'.

In the second statement, I use something like parameter[0] to find if the first index of that string is equal to 'h' or 'a'; or parameter[1] to examine if the second index is equal to '1' or '8'. The statement will help you find the 'Border'

Lastly, everything else is 'Inside'

answered by (1 point)
+3 votes

Start with an if for four the corners then two more elifs for the borders(because they'll be either at a or h or 1 or 8 and ur final else for inside.

answered by (1 point)