Reputation: 21
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)
The above stuff gives
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
I've got 9 values in my tuple or list and 9 curly brackets. Right?
Upvotes: 2
Views: 1064
Reputation: 24052
The format
method expects individual arguments rather than a single list. But you can easily fix it by changing:
"...".format(board)
to:
"...".format(*board)
The asterisk *
will cause the list elements to be passed as individual arguments.
Upvotes: 9
Reputation: 77827
You have only one item in the values list for the print
-- a list.
You need to split out the individual integers.
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]
))
Upvotes: 0
Reputation: 3054
I don't think 'format' works like that with lists. You would need something like this I believe.
x=[1,2]
print('one + {} = {}'.format(x[0],x[1]))
Upvotes: 0