【问题标题】:ZeroDivisionError: division by zero in chess.pyZeroDivisionError:在 chess.py 中除以零
【发布时间】:2020-12-20 08:20:09
【问题描述】:

我创建了一个国际象棋观看视频,完成后出现错误

ZeroDivisionError: 除以零

我的整个代码:https://onlinegdb.com/HJR_9t2nD

board = [ ['Rb', 'Nb', 'Bb', 'Qb', 'Kb', 'Bb', 'Nb', 'Rb'], #8
          ['Pb', 'Pb', 'Pb', 'Pb', 'Pb', 'Pb', 'Pb', 'Pb'], #7
          [  0,    0,    0,    0,    0,    0,    0,    0],  #6
          [  0,    0,    0,    0,    0,    0,    0,    0],  #5
          [  0,    0,    0,    0,    0,    0,    0,    0],  #4
          [  0,    0,    0,    0,    0,    0,    0,    0],  #3
          ['Pw', 'Pw', 'Pw',  'Pw', 'Pw', 'Pw', 'Pw', 'Pw'], #2
          ['Rw', 'Nw', 'Bw',  'Qw', 'Kw', 'Bw', 'Nw', '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,x,y,enemy)
  File "chess.py", line 496, in isAttackedby
    findPossibleSquares(position,x,y,True)) #The true argument
  File "chess.py", line 569, in findPossibleSquares
    if isOccupiedby(board,kx,y,enemy_color):
  File "chess.py", line 458, in isOccupiedby
    if board[x / y]==0:
ZeroDivisionError: division by zero

【问题讨论】:

  • 是x和y棋盘坐标吗?
  • @Rabbid 我说的是函数参数不是完整的代码

标签: python pygame chess


【解决方案1】:

/ 是除法运算符。如果除数为零,则会出现异常。

您很可能尝试访问网格的某个元素。如果board 是列表列表,则必须连续使用两次订阅运算符。由于网格是按行组织的,所以第一个订阅索引是y,第二个是x

if board[x / y]==0:

if board[y][x]==0:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多