我必須編寫一個函數(def)來檢查我編寫的索引是否在2d列表中。如果是,則必須返回True,否則必須返回False。檢查索引是否在2d列表中[Python]
def height(board):
height=0
for x in board:
height+=1
return height
def width(board):
wid=len(board[0])
return wid
def on_board(board, x, y):
check=False
x1 = int(x)
y1 = int(y)
for x,y in board:
if x1 > height(board) or y1 > width(board):
check=False
else:
check=True
return check
名單看起來是這樣的:
board = [["V1", "V1"],
["V2", "V2"],
["V3", "V3"]]
作爲一個建議,OP可以創建一個'Board'類並在其中封裝一些這些細節。 – ray
我得到這個錯誤: board [int(x)] [int(y)] IndexError:列表索引超出範圍 –
我必須編輯:除了ValueError,IndexError:除了ValueError爲IndexError。因爲:「Python 3.4版本不支持此語法。」 –