我是堆棧溢出的新手,我正在用python編寫這個程序,它應該創建兩個不同的遊戲板對象:一個小的和一個大的。兩個遊戲板對象都是由列表組成的,所以我稍後可以通過索引訪問板上的特定空間。在較大的板子上沒有任何明顯的錯誤,但是當我創建小板對象並嘗試通過索引訪問它們的特定空間時,我得到錯誤「對象不支持索引」。我真的很困惑,因爲它與大董事會一起工作,我已經通過其他大量關於這個錯誤的帖子,但他們似乎都發現對象中的錯誤不是實際列表。不過,我在構造函數中定義它應該是一個列表。我希望你們能幫忙!一個列表對象不支持索引,但另一個呢?
下面是類的代碼:
import copy
SMALL_BOARD = [ #one list for small board
None, None, None,
None, None, None,
None, None, None,
]
BIG_BOARD = [ #one big list for big board
None, None, None, None, None, None,
None, None, None, None, None, None,
None, None, None, None, None, None,
None, None, None, None, None, None,
None, None, None, None, None, None,
None, None, None, None, None, None,
]
class SMALLBoard():
def __init__(self, board=None):
if board is None:
board = copy.deepcopy(SMALL_BOARD)
self.sboard = board
class BIGBoard():
def __init__(self, bboard=None):
if bboard is None:
bboard = copy.deepcopy(BIG_BOARD)
self.bboard = bboard
def equal_small_boards(self, boardA, boardB, boardC, boardD):
tempboard=BIGBoard()
tempboard[0]= boardA[0]
tempboard[3]= boardB[0]
tempboard[18]= boardC[0]
tempboard[21]= boardD[0]
return tempboard
當我運行下面的代碼我得到的錯誤:在equal_small_boards tempboard [0] = boardA [0] 類型錯誤: 'SMALLBoard' 對象不不支持索引'
bboard = BIGBoard()
sboardA = SMALLBoard()
sboardB = SMALLBoard()
sboardC = SMALLBoard()
sboardD = SMALLBoard()
bboard=bboard.equal_small_boards(sboardA, sboardB, sboardC, sboardD)
感謝您提前給出答案! :)
'tempboard'是一個'BIGBoard','BIGBoard'不支持索引。 –