AttributeError: 'NoneType' object has no attribute 'symbol' python-chess

339 views Asked by At

I'm working on rating positions but I don't know if a piece is on a certain square. when I run my code I get

AttributeError: 'NoneType' object has no attribute 'symbol'

Here is the code:

for i in range(0,8):
        for j in range(0,8):
            if i == 0:
                s='A'
            if i == 1:
                s='B'
            if i == 2:
                s='C'
            if i == 3:
                s='D'
            if i == 4:
                s='E'
            if i == 5:
                s='F'
            if i == 6:
                s='G'
            if i == 7:
                s='H'
            if j == 0:
                e='1'
            if j == 1:
                e='2'
            if j == 2:
                e='3'
            if j == 3:
                e='4'
            if j == 4:
                e='5'
            if j == 5:
                e='6'
            if j == 6:
                e='7'
            if j == 7:
                e='8'
            piece = board.piece_at(getattr(chess,s+e))
            print(piece.symbol())
2

There are 2 answers

0
LittleCoder On

Where there is no piece (like on e4 in the starting board), board.piece_at() will return None. So you should check if the piece == None before asking its symbol.

Note that your code can be more efficient like this :

import chess
board = chess.Board()
for i in chess.SQUARES :
    piece = board.piece_at(i)
    if piece != None :
        print(piece.symbol())
0
LittleCoder On

If tou want to rate position in chess with python, I can help you a little. I've also worked on.
You can have a table of good positons for each piece (see theis page). You can create list of goods positions for each piece, starting from a1, b1 ... and ending by h8.
e.g. for the white King :

K = [0.269, 0.883, 1.907, 9.062, 3.559, 6.985, 2.233, 0.495,
    0.435, 1.236, 3.644, 9.126, 11.745, 8.228, 2.583, 0.0737,
    0.553, 1.564, 2.329, 3.557, 3.772, 3.142, 2.056, 0.774,
    0.505, 0.877, 1.233, 1.369, 1.456, 1.432, 1.118, 0.592,
    0.370, 0.672, 0.712, 0.686, 0.763, 0.748, 0.728, 0.399,
    0.246, 0.418, 0.481, 0.440, 0.395, 0.440, 0.398, 0.288,
    0.164, 0.263, 0.262, 0.200, 0.190, 0.215, 0.259, 0.167,
    0.066, 0.106, 0.080, 0.086, 0.076, 0.082, 0.088, 0.056]

Note : For black pieces, values must be negatives, for a good analysis.

Next, you can create a dict to refer each piece string symbol to its corresponding list of goods positions :

pieces = {'K': K, 'k': k, 'Q': Q, 'q': q, 'R': R, 'r': r, 'N': N, 'n': n, 'B': B, 'b': b, 'P': P, 'p': p}

And now, you are able to have a rating estimation of the position (just for placement, not for other parameters, but if you contact me, I'll can help you on other position rating in chess. I've understand your question as "I want to rate each piece placement on the chessboard") :

def placement_eval(pos) :
    '''evaluation of piece placement'''
    evalu = 0
    for case in list(chess.SQUARES) :
        piece = pos.piece_at(case)
        if piece != None :
            evalu = evalu + pieces[piece.symbol()][case]
return evalu / 10 # /10  or /100, as you want