[Tutor] Having trouble with " table % listTable.values() " line
Jason Massey
jason.massey at gmail.com
Fri Nov 24 15:04:40 CET 2006
So you want to convert a list into a tuple.
Here ya go, using your example of the table setup:
>>> raw_table ="""
%s | %s | %s
--------------
%s | %s | %s
--------------
%s | %s | %s
"""
>>> from random import choice
>>> x_o = [choice(['x','o']) for i in range(9)]
>>> x_o
['x', 'x', 'x', 'x', 'o', 'o', 'o', 'x', 'x']
>>> filled_table = raw_table % tuple(x_o)
>>> print filled_table
x | x | x
--------------
x | o | o
--------------
o | x | x
>>>
On 11/24/06, Mihai Iacob <aymchaos at yahoo.com> wrote:
>
> Hello,
>
> This is my first post so if i make any kind of
> mistakes take it easy on me :).
>
> I don't know how to explain clearly what is the
> problem but i'll try.
> table = """
> %s |%s |%s
> -----------
> %s |%s |%s
> -----------
> %s |%s |%s
> """
>
> listTable is a dictionary.
>
> listTable = {0 :'X', 1 :' ' , 2 :' ', 3 :'O', 4 :' ',
> 5 :' ', 6 :'O', 7 :' ', 8 :'X',}
>
> I am trying to take the values from the dictionary
> listTable.values() and print them in the table.
>
> The problem is that listTable.values() returns a list
> " ['X', ' ', ' ', 'O', ' ', ' ', 'O', ' ', 'X'] "
> and python won't assign the values of the dictionary
> in the table. " table % listTable.values() "
>
> This error message appears:
>
> Traceback (most recent call last):
> File "<pyshell#81>", line 1, in <module>
> table % listTable.values()
> TypeError: not enough arguments for format string
>
>
> So instead of the list " ['X', ' ', ' ', 'O', ' ', '
> ', 'O', ' ', 'X'] " which won't let me assign strings
> in the table, i want to get ('X', ' ', ' ', 'O', ' ',
> ' ', 'O', ' ', 'X') from listTable.values()
>
> I know i can make it work using:
> table % (listTable[0],listTable[1],
> listTable[2],listTable[3],listTable[4],
> listTable[5],listTable[6],listTable[7],listTable[8])
>
> but it's kinda long.
>
>
> If somebody can tell me how to convert the []list into
> () i'll make him a little shrine and worship him for 5
> seconds :P
>
> I'm trying to make a OXO game, so don't give me any
> hints even if you spot mistakes (i hope i can make it
> alone)
>
>
>
>
>
>
> ____________________________________________________________________________________
> Do you Yahoo!?
> Everyone is raving about the all-new Yahoo! Mail beta.
> http://new.mail.yahoo.com
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061124/d3cbb8b3/attachment.html
More information about the Tutor
mailing list