问题描述
ZeroDivisionError: 除以零
我的全部代码:https://onlinegdb.com/HJR_9t2nD
board = [ ['Rb','Nb','Bb','Qb','Kb','Rb'],#8
['Pb','Pb','Pb'],#7
[ 0,0],#6
[ 0,#5
[ 0,#4
[ 0,#3
['Pw','Pw','Pw'],#2
['Rw','Nw','Bw','Qw','Kw','Rw'] ]#1
# a b c d e f g h
def isOccupiedby(board,x,y,color):
if board[x / y]==0:
#the square has nothing on it.
return False
这里是完整的错误
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\python37\lib\threading.py",line 926,in _bootstrap_inner
self.run()
File "C:\python37\lib\threading.py",line 870,in run
self._target(*self._args,**self._kwargs)
File "chess.py",line 1054,in negamax
moves = allMoves(position,colorsign)
File "chess.py",line 842,in allMoves
listofpieces = getallpieces(position,color)
File "chess.py",line 831,in getallpieces
if isOccupiedby(board,i,j,color):
File "chess.py",line 458,in isOccupiedby
if board[x / y]==0:
ZeroDivisionError: division by zero
Traceback (most recent call last):
File "chess.py",line 1713,in <module>
createShades([])
File "chess.py",line 955,in createShades
if isCheck(position,'white'):
File "chess.py",line 801,in isCheck
return isAttackedby(position,enemy)
File "chess.py",line 496,in isAttackedby
findPossibleSquares(position,True)) #The true argument
File "chess.py",line 569,in findPossibleSquares
if isOccupiedby(board,kx,enemy_color):
File "chess.py",in isOccupiedby
if board[x / y]==0:
ZeroDivisionError: division by zero
解决方法
/
是除法运算符。如果除数为零,则会出现异常。
您很可能试图访问网格的元素。如果 board
是列表列表,则必须连续使用订阅运算符两次。由于网格是按行组织的,第一个订阅索引是 y
,第二个是 x
:
if board[x / y]==0:
if board[y][x]==0: