2014-03-26 127 views
0

我已經找過這樣的其他案例,但沒有一個適用於我....或者如果他們這樣做,我不明白如何。TypeError:'int'對象不是可訂閱的

我所試圖做的是通過一個二維數組(這一點)來篩選:

blocks = [ 
[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ], 

[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ], 

[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ], 

[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ], 

[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ], 

[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ], 

[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ], 

[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ], 

[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ], 

[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ] 

,並找出當前的部分是什麼。對不起,如果我的代碼有點草率,我是python的新手。這是我的代碼(部分),

def drawBlock(): 
for blockL1 in blocks: 
    for blokL2 in blocks: 
     if blocks[blockL1[blockL2[1]]] == 0: 
      pygame.draw.rect(screen, (0, 255, 255), Rect((blockL1 * 32, blockL2 *32), (32, 32))) 
      pygame.draw.rect(screen, color, Rect(i * 32,)) 
     global blockL2 
     blockL2 += 1 
    global blockL1 
    blockL1 += 1 
+0

您的塊實際上是一個3維數組。移除每個「0」周圍的'[]'。 – GWW

回答

0

你有幾個問題在這裏進行。

主要問題是您使用for blank in blank_holder

當你寫

for blockL1 in blocks: 

它幾乎等同於編寫

for ii in range(len(blocks)): 
    blockL1 = blocks[ii] 
    ... 

意味着blockL1不是一個整數,或塊的指標..它是塊的元素!

它的值將如下所示:[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ]

這是一個列表的列表...其中作爲blocks是包含列表列出的名單...是啊

當你寫

for blockL1 in blocks: 
    for blockL2 in blocks: 

blockL2的價值將是一個單獨的列表[0]

假設你想看看在包含在blockL2的一個值,你可以改變:

if blocks[blockL1[blockL2[1]]] == 0: 

到:

if blockL2[0] == 0: # the 1 index would go out of bounds for a list length 1 

但是這個代碼看起來像你需要的blockL1blockL2的索引值,所以我建議你遍歷列表(意思是ints)...不是他們的elemnents

def drawBlock(): 
    for blockL1 in range(len(blocks)): 
     for blockL2 in range(len(blockL1)): 
      if blocks[blockL1[blockL2[0]]] == 0: 
       pygame.draw.rect(screen, (0, 255, 255), Rect((blockL1 * 32, blockL2 *32), (32, 32))) 

希望這有助於一些!

+0

謝謝!這實際上是有道理的!我很感謝你試圖幫助我,因爲我學到的第一種循環是JavaScript風格,這使得很難設想出不同的風格。所以你建議我使用底部的代碼?謝謝! – ETBCOR

+0

@ETBCOR它真的取決於你要去的風格,但是如果邏輯需要知道當前塊在主數組中的位置,我建議底部。 – flakes

相關問題