所以我使用這個小代碼片段,顯然索引超出範圍?我看不出爲什麼,但它給了我錯誤。python IndexError:超出範圍?
Grid = [[0,0,0,0],[0,0,0,0]]
def SpawnNumber(Grid):
# Check is true when the random grid space is a 0
check = False
# Loop until an empty space is chosen
while check == False:
rn1 = randint(0,3)
rn2 = randint(0,3)
print(rn1)
print(rn2)
# If the space is empty, fill it
if Grid[rn1][rn2] == 0:
Grid[rn1][rn2] = 2
check = True
return(Grid)
網格應該有兩個維度,每個維度從0-3變化,爲什麼randint(0,3)超出範圍?
'rn1'應該只從0-1。你只有兩個子列表。 –