發生此錯誤。我不知道它是什麼。我是一名初學者程序員。列表索引超出範圍ConnectFour
ConnectFourBoard.py", line 58, in show_board_dynamic print(self.__board[i][j], end = ""), IndexError: list index out of range
我的代碼:
class ConnectFourBoard:
def __init__(self, cols = 7, rows = 6, requiredToWin = 4):
global __board
self.__space = ''
self.__board = []
self.cols = cols
self.rows = rows
self.Win = requiredToWin
self.__board = [[''] * rows for i in range(cols)]
for i in range(cols):
row = ['']*cols
self.__board.append(row)
def MakeMove(self, row, col, element):
global __board
self.__board[row][col] = element
def CheckForWin(self):
return winner
def CheckHz():
for x in range (6):
for y in range (7):
row += board[x][y]
print("%s" %row)
row = ""
if "XXXX" in row:
print("Winner is X")
break;
def CheckVt():
for y in range(7):
for x in range(6):
column += board[y][x]
print("%s" %column)
column = ""
if "XXXX" in column:
print ("Winner is X")
break;
def FullBoard(self):
return True
def FreeSpace(self, row, col):
return True
def show_board_dynamic(self):
print()
print("-------")
for i in range(len(self.__board)):
for j in range(len(self.__board)):
print("|", end = "")
print(self.__board[i][j], end = ""),
print("|")
print("-------")
print()
退房http://pythontutor.com –
嘗試刪除該'因爲我在範圍內(的cols): 行= [ ''] *的cols 自.__ board.append(行)'從'__init __() ',即它的最後一行。 –