[Tutor] Key Error
Bob Gailer
bgailer at alum.rpi.edu
Sun Jul 8 18:33:55 CEST 2007
Sara Johnson wrote:
>
> Probably best if I skip the example and show what code I do have:
> ~~~~~~~~~~~
> for key in h.keys():
> wssd=h[key]['WSSD']
> wspd=h[key]['WSPD']
> wmax=h[key]['WMAX']
> newi=h[key]['NEWI']
> if wssd<-989. or wspd<-989. or wmax<-989.: break
> if wspd==0.: break
> ~~~~~~~~~~~~~~~~~
> Where the "newi" = "abcd" that I showed before. I have no where else
> in my code anything pertaining to these 4 keys. The first 3 were
> there, and produce no errors. I am making adjustments to an existing
> script. I only have C programming knowledge so my thought was that it
> "newi" was just a variable that needed to be assigned. You'll notice
> the parameters below (i.e., if wssd < -989 ) but there is obviously
> nothing for "newi" at the moment. The program's only error at the
> moment seems to be this line:
>
> newi=h[key]['NEWI']
> But as you can see, the other items are set up the same way.
Most likely h is a dictionary, and the values are also dictionaries. At
least one of the value dictionaries has no key "NEWI".
To see what h is, put:
print h
before the for statement. You should see something like:
{'somekey': {'WSSD': 3, 'WSPD': 4, 'WMAX': 5, 'NEWI': 6}, 'anotherkey': ...}
Python uses braces with key : value pairs to represent a dictionary.
If that does not help you, post the result (or attach if it is really long).
Then we need to find how h itself was created. Any clues about that?
--
Bob Gailer
510-978-4454 Oakland, CA
919-636-4239 Chapel Hill, NC
More information about the Tutor
mailing list