I'm relatively new to coding, so any criticism is appreciated. I just finished chapter 5 of Automate the Boring Stuff and started the practice projects of the chapter. I've looked at other codes on the same project to see their takes on it, and to see how I can do it. But the code keeps returning False as the value, and I'm not sure where the issue is.
import string
chessboard = {'1h': 'bking', '6c': 'wqueen', '2g': 'bbishop', '5h': 'bqueen', '3e': 'wking'}
def isValidChessBoard(chessboard):
# Define the components
bpieces, wpieces = 0, 0
bking, wking, bpawn, wpawn = 0, 0, 0, 0
valid = True
coord = True
# Check if board has exactly 1 black and white king.
if bking and wking != 1:
valid = False
# Check if there are at most 16 pieces, at most 8 pawns, and must be within '1a' and '8h'.
if bpieces or wpieces > 16:
valid = False
if bpawn or wpawn > 8:
valid = False
for x in range(1,9):
for y in list(map(chr,range(ord('a'), ord('h')+1))):
coord = True
# Check if the pieces begins with 'b' or 'w'; followed by 'pawn', 'knight', 'bishop', 'rook', 'queen' or 'king'.
for piece in chessboard.values():
if piece != 'b' or 'w' + 'pawn' or 'knight' or 'bishop' or 'rook' or 'queen' or 'king':
valid = False
if valid and coord:
return True
else:
return False
print(isValidChessBoard(chessboard))
I think the issue is around here:
if piece != 'b' or 'w' + 'pawn' or 'knight' or 'bishop' or 'rook' or 'queen' or 'king':
valid = False
But I'm not sure, with how little experience I have with codes.