start = [0,0,0,0,0,0,0,0,0]
def print_board(turn, board):
print(turn + " turn" + "\n {}{}{}\n {}{}{}\n {}{}{}".format(board))
current_turn = "your"
print_board(current_turn, start)
以上内容给出了
Traceback (most recent call last):
File "so.py", line 5, in <module>
print_board(current_turn, start)
File "so.py", line 3, in print_board
print(turn + " turn" + "\n {}{}{}\n {}{}{}\n {}{}{}".format(x for x in board))
IndexError: tuple index out of range
我的元组或列表中有9个值,9个大括号。正确?
答案 0 :(得分:9)
format
方法需要单个参数而不是单个列表。但您可以通过更改:
"...".format(board)
为:
"...".format(*board)
星号*
将使列表元素作为单独的参数传递。
答案 1 :(得分:0)
print
的值列表中只有一个项目 - 列表。
你需要拆分各个整数。
def print_board(turn, board):
print(turn + " turn" + "\n {}{}{}\n {}{}{}\n {}{}{}".format(
board[0], board[1], board[2],
board[3], board[4], board[5],
board[6], board[7], board[8]
))
答案 2 :(得分:0)
我不认为'格式'与列表一样。我相信你需要这样的东西。
x=[1,2]
print('one + {} = {}'.format(x[0],x[1]))