2
這是我試圖運行代碼列表索引錯誤類型
import random
def createBoard(rows, columns, mines):
board = [["C" for row in range(rows)],[" " for col in range(columns)]]
for i in range(0, mines):
board[[int(random.randrange(rows))],[int(random.randrange(columns))]] = "C", "*"
return board
createBoard(9, 12, 10)
board = board
print(board)
這是錯誤我得到
Traceback (most recent call last):
File "C:\Users\Josh\Desktop\Python\createBoard.py", line 7, in <module>
createBoard(9, 12, 10)
File "C:\Users\Josh\Desktop\Python\createBoard.py", line 5, in createBoard
board[[int(random.randrange(rows))],[int(random.randrange(columns))]] = "C", "*"
TypeError: list indices must be integers, not tuple
'board [[int(random.randrange(rows))],int(random.randrange(columns))]]',在這裏創建一個元組。有兩個列表(一個是'[int(random.randrange(rows))]'',另一個是'[int(random.randrange(columns))]')。 –