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())
Where there is no piece (like on e4 in the starting board),
board.piece_at()
will returnNone
. So you should check if thepiece == None
before asking its symbol.Note that your code can be more efficient like this :