2
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?

3
  • Can you clarify your code a bit? Maybe use the code block? Commented Nov 3, 2017 at 19:52
  • I want to make a very ugly and stupid tic tac toe program so I want to store the board ( idk how else to call that) in one list instead of 9 different variables Commented Nov 3, 2017 at 19:57
  • When you get to a resolution, please remember to up-vote useful things and accept your favourite answer (even if you have to write it yourself), so Stack Overflow can properly archive the question. Commented Nov 3, 2017 at 20:24

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

that is handy to know
0

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]))

Comments

0

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]
    ))

2 Comments

Thank you, but how do I split them out?
Thnx everyone! It worked with the "*" before the list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.