vendredi 27 août 2021

Why is my for loop only printing the first if statement repeatedly? [duplicate]

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

Aucun commentaire:

Enregistrer un commentaire