Keyerror problem

davbucko davbucko at deletethis.yahoo.com
Sun Sep 1 20:33:44 EDT 2002


Hi guys,

I have a feeling this is going to turn out to be a pretty stupid question,
but here goes anyway...

I have a class Flags as follows:

class Flags:
    def __init__(self):
        self.flags = {}

    def getInverseFlags(self):
        for i in self.flags.keys():
            #switch some keys/values around

    def setFlag(self,name,value):
        self.flags[`name`] = value
#End of class Flags

I then call that class and use it:
flags = Flags()
powers = Powers() #This class is working fine elsewhere, so I don't think
that it is the problem

def getComplimentaryGrid():
    flags.getInverseFlags()
    for j in powers.dict[flags.flags['row1']]: #This line doesn't work
For some reason, on the line above, I am getting the following error
message:

Traceback (most recent call last):
  File "C:\Dave\python\squaresprint.py", line 188, in ?
    iterateGrids()
  File "C:\Dave\python\squaresprint.py", line 163, in iterateGrids
    grid.checkGridCols()
  File "C:\Dave\python\squaresprint.py", line 78, in checkGridCols
    getComplimentaryGrid()
  File "C:\Dave\python\squaresprint.py", line 167, in getComplimentaryGrid
    for j in powers.dict[flags.flags['row1']]:
KeyError: row1

I don't know why this is, as 'row1' is clearly a key.  If I enter the
following just above the line that is giving me trouble above:

for i in flags.flags.keys():
    print i,

I get the output: 'col1' 'col3' 'col2' 'row3' 'row2' 'sum' 'row1' which
shows that 'row1' is a key.

I am entirely new to python, so if this is an idiotic question, please say.
If you need more code, there (obviously) is more, but imvho it is not
causing any problems.

Cheers
Dave





More information about the Python-list mailing list