[Tutor] Key Error

Bob Gailer bgailer at alum.rpi.edu
Sun Jul 8 23:34:15 CEST 2007


Sara Johnson wrote:
> Should I temporarily comment out the other values (i.e., WSSD, 
> WSPD...)?  I'm getting an error now that says:
>  
>   File "./mymods.py", line 122
>     if wssd<-989. or wspd<-989. or wmax<-989.: break
>     ^
> SyntaxError: invalid syntax
Please post (always) the surrounding code. The error is likely due to 
whatever is on the preceding line.

Here's your original code:

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

I was suggesting you replace all of that with:

print len(h)
for key in h.keys():
    try:
        newi=h[key]['NEWI']
    except KeyError:
        print key

Keep in mind that Python uses indentation to convey structure instead of braces (like C).


-- 
Bob Gailer
510-978-4454 Oakland, CA
919-636-4239 Chapel Hill, NC




More information about the Tutor mailing list