0

I am trying to create a list representing a chess board. I have this code for it:

#possible list items
EMPTY = "-"
ROOK = "ROOK"
KNIGHT = "KNIGHT"
BISHOP = "BISHOP"
QUEEN = "QUEEN"
KING = "KING"
PAWN = "PAWN"
board = []

royal_row = [] #list for row containing king

pawn_row = []#list for pawn row

row = [] #generic row

#setting the board
for i in range(8):
    if i == 0 or 7:
        royal_row = [ROOK,KNIGHT,BISHOP,QUEEN,KING,BISHOP,KNIGHT,ROOK]
        board.append(royal_row)
        
    elif i == 1 or 6:
        pawn_row = [PAWN for i in range(8)]
        board.append(pawn_row)
        
    else: 
        row = [EMPTY for i in range(8)]
        board.append(row)

for i in range(8):
    print(board[i], end = "\n")

When I run this code it just prints:

['ROOK', 'KNIGHT', 'BISHOP', 'QUEEN', 'KING', 'BISHOP', 'KNIGHT', 'ROOK']

['ROOK', 'KNIGHT', 'BISHOP', 'QUEEN', 'KING', 'BISHOP', 'KNIGHT', 'ROOK']

['ROOK', 'KNIGHT', 'BISHOP', 'QUEEN', 'KING', 'BISHOP', 'KNIGHT', 'ROOK']

['ROOK', 'KNIGHT', 'BISHOP', 'QUEEN', 'KING', 'BISHOP', 'KNIGHT', 'ROOK']

['ROOK', 'KNIGHT', 'BISHOP', 'QUEEN', 'KING', 'BISHOP', 'KNIGHT', 'ROOK']

['ROOK', 'KNIGHT', 'BISHOP', 'QUEEN', 'KING', 'BISHOP', 'KNIGHT', 'ROOK']

['ROOK', 'KNIGHT', 'BISHOP', 'QUEEN', 'KING', 'BISHOP', 'KNIGHT', 'ROOK']

['ROOK', 'KNIGHT', 'BISHOP', 'QUEEN', 'KING', 'BISHOP', 'KNIGHT', 'ROOK']

my expected output is:

['ROOK', 'KNIGHT', 'BISHOP', 'QUEEN', 'KING', 'BISHOP', 'KNIGHT', 'ROOK']

['PAWN', 'PAWN', 'PAWN', 'PAWN', 'PAWN', 'PAWN', 'PAWN', 'PAWN']

['-', '-', '-', '-', '-', '-', '-', '-']

['-', '-', '-', '-', '-', '-', '-', '-']

['-', '-', '-', '-', '-', '-', '-', '-']

['-', '-', '-', '-', '-', '-', '-', '-']

['PAWN', 'PAWN', 'PAWN', 'PAWN', 'PAWN', 'PAWN', 'PAWN', 'PAWN']

['ROOK', 'KNIGHT', 'BISHOP', 'QUEEN', 'KING', 'BISHOP', 'KNIGHT', 'ROOK']

If I separate the 1, 0, 6, and 7 to their own elif clause then it prints correctly but I want to understand why the original code doesn't

2 Answers2

0

if i == 0 or 7:

This statement is essentially the below

if i == 0 or 7 != 0

So, because 7 is always greater than 1, it is always true.

To fix this, try the following instead

if i == 1 or i == 7

HPringles
  • 1,042
  • 6
  • 15
0

You decide to change in the if's and put: i == 0 or i == 7 and i == 1 or i == 6

#possible list items
EMPTY = "-"
ROOK = "ROOK"
KNIGHT = "KNIGHT"
BISHOP = "BISHOP"
QUEEN = "QUEEN"
KING = "KING"
PAWN = "PAWN"
board = []

royal_row = [] #list for row containing king

pawn_row = []#list for pawn row

row = [] #generic row

#setting the board
for i in range(8):
    if i == 0 or i == 7:
        royal_row = [ROOK,KNIGHT,BISHOP,QUEEN,KING,BISHOP,KNIGHT,ROOK]
        board.append(royal_row)

    elif i == 1 or i == 6:
        pawn_row = [PAWN for i in range(8)]
        board.append(pawn_row)

    else:
        row = [EMPTY for i in range(8)]
        board.append(row)

for i in range(8):
    print(board[i], end = "\n")
Piero
  • 404
  • 3
  • 7
  • 22
  • Thanks !I did that and it fixed the issue. Do you know why writing it the way I originally did would cause that line to repeat itself? – Binomial_Gnome Aug 27 '21 at 19:56
  • @Binomial_Gnome I wrote it at the beginning. before the code. When you did the if you didn't put equality for both numbers. You have to do: `if i == 0 or i == 7` and also `elif i == 1 or i == 6` – Piero Aug 27 '21 at 20:00