這是我的代碼:如何在Python中填充二維數組?
empty = ' '
board = [[empty, empty, empty], [empty, empty, empty], [empty, empty, empty]]
def print_board():
for idx, val in enumerate(board):
print(val)
def board_update(x,y,player):
board[x:y] = player;
game_is_running = True
player = 'x'
print_board()
while game_is_running:
if player == 'x':
print ('x turn')
ver = int(input('input vertical of x '))
hor = int(input('input horizontal of x '))
board_update(ver, hor, player)
player = 'o'
print_board()
if player == 'o':
print ('o')
vertical = int(input('input vertical of o '))
horizontal = int(input('input horizontal of o '))
每當我運行的代碼,它只是放在這樣的信:
[' ', ' ', ' ']
x
[' ', ' ', ' ']
[' ', ' ', ' ']
我如何使它看起來像這樣:
[' ', ' ', ' ']
[' ', x , ' ']
[' ', ' ', ' ']
'board [x:y] = player'不會做你認爲它做的事。你需要'board [x] [y] = player'。 – L3viathan