Reputation: 13
While i was trying to build a tic tac toe game and i wanted to test my function for displaying the board. It gave me 'tuple index out of range' error constantly so i changed it to the simplest form, i simply put a list in the format method:
print(' {7} | {8} | {9} \n ------------ \n {4} | {5} | {6} \n ----------- \n {1} | {2} | {3} '.format(['#','X','O','X','O','X','O','X','O','X']))
the list has 10 elements. And there is no tuple in sight i am a beginner in python and i am currently very struggling with this.
Upvotes: 2
Views: 67
Reputation: 23074
You are passing a single argument to format
. Remove the list [ ]
to pass ten arguments.
Or you can use *
unpacking operator to unpack one iterable sequence into multiple arguments.
print(
' {7} | {8} | {9} \n'
'-----------\n'
' {4} | {5} | {6} \n'
'-----------\n'
' {1} | {2} | {3} '.format(*'#XOXOXOXOX')
)
Upvotes: 0
Reputation: 78770
Currently, you are giving format
a single argument - a list with ten elements.
Give format
ten arguments by unpacking the list or not using a list at all.
with unpacking:
>>> print(' {7} | {8} | {9} \n ------------ \n {4} | {5} | {6} \n ----------- \n {1} | {2} | {3} '.format(*['#','X','O','X','O','X','O','X','O','X']))
X | O | X
------------
O | X | O
-----------
X | O | X
without unpacking:
>>> print(' {7} | {8} | {9} \n ------------ \n {4} | {5} | {6} \n ----------- \n {1} | {2} | {3} '.format('#','X','O','X','O','X','O','X','O','X'))
X | O | X
------------
O | X | O
-----------
X | O | X
Upvotes: 1
Reputation: 599876
.format
expects individual arguments, not an iterable. You need to unpack the list into separate args using the *
operator.
print(' {7} ...'.format(*['#','X','O','X','O','X','O','X','O','X']))
# ^ here
Upvotes: 0
Reputation: 2022
.format
works without list.
print(' {7} | {8} | {9} \n ------------ \n {4} | {5} | {6} \n ----------- \n {1} | {2} | {3} '.format(
'#', 'X', 'O', 'X', 'O', 'X', 'O', 'X', 'O', 'X'))
Upvotes: 0