[Tutor] What's the invalid syntax? Code supplied

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Aug 5 23:09:45 CEST 2005



> What's the invalid syntax? Here is the code:


Hi Nathan,

You need to have a comma between the items of a dictionary.  For example:

######
>>> numerals = {
...     0 : 'zero',
...     1 : 'one',
... }
>>>
>>> numerals[0]
'zero'
>>> numerals[1]
'one'
######


Some comments on your code.  I notice that you're using strings as the
keys of your dictionaries, as in:

######
types = {
    '0' : ["Diamonds"],
    '1' : ["Hearts"],
    '2' : ["Spades"],
    '3' : ["Clubs"],
     }
######

(with the comma bug corrected).


However, you can use numbers as your keys, too, and this will greatly
simpify some of the dictionary-selection code.  For example:

######
if index == 0:
    index = hand['0']
if index == 1:
    index = hand['1']
if index == 2:
    index = hand['2']
if index == 3:
    index = hand['3']
if index == 4:
    index = hand['4']
...
#######

can be reduced from many statements to a single dictionary lookup
statement.



More information about the Tutor mailing list