我试图通过这样做来改变列表中的元素:
board = []
for i in range(3):
board.append("-"*3)
def print_board(board):
for row in board:
print " ".join(row)
print_board(board)
print "Welcome to tic tac toe. You go first because I say so."
column = int(raw_input("Enter column number: "))
row = int(raw_input("Enter row number: "))
board[row][column] = "O" #THIS ONE
print_board(board)
但是错误消息" TypeError:' str'对象不支持项目分配"出现。我该怎么办?
答案 0 :(得分:3)
'-' * 3
是一个字符串,你想要一个列表。
所以你应该替换它:
board.append("-"*3) # '---'
用这个:
board.append(['-']*3) # ['-','-','-']