elif self.game.puzzle [row] [col] == 0:TypeError:list indices必须是整数或切片,而不是float

时间:2017-12-04 03:16:43

标签: python-3.x tkinter

我正在学习如何在数独中实现AI算法。但我的教授需要一个界面,并且我对tkinter python 3.6不熟悉。我在制作数独python游戏时遵循了newcoder.io教程的代码,但我得到了一个错误,你可以看到标题。这是我的完整代码。请帮忙。

我无法点击一个单元格并在玩游戏时设置一个数字并出错。

def __cell_clicked(self, event):
        if self.game.game_over:
            return
        x, y = event.x, event.y
        if (MARGIN < x < WIDTH - MARGIN and MARGIN < y < HEIGHT - MARGIN):
            self.canvas.focus_set()

            # get row and col numbers from x,y coordinates
            row, col = (y - MARGIN) / SIDE, (x - MARGIN) / SIDE

            # if cell was selected already - deselect it
            if (row, col) == (self.row, self.col):
                self.row, self.col = -1, -1
            elif self.game.puzzle[row][col] == 0:
                self.row, self.col = row, col
        else:
            self.row, self.col = -1, -1

        self.__draw_cursor()

我收到此错误消息:

"Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "sudoku.py", line 141, in __cell_clicked
    elif self.game.puzzle[row][col] == 0:
TypeError: list indices must be integers or slices, not float"

0 个答案:

没有答案