[CentralOH] Python Puzzler -- The little max bug

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Wed Mar 11 02:04:16 CET 2015


On Tue, 10 Mar 2015 19:38:48 -0400, Eric Floehr <eric at intellovations.com> wrote:

> What was the bug, and why was MAXT the only formula type affected?

'MAXT' was the first type for each hour,
so when formula_type was 'MAXT', 
try: hourdict = hoursdict[hour] failed with an KeyError, 
so the except section runs. hourdict and hoursdict[hour] 
are initialized to (different) empty dictionaries and 
hoursdict[hour] does not refer to hourdict, 
so changes to hourdict are not saved in hoursdict. 
For subsequent lines of an hour, the hourdict = hoursdict[hour]
succeeds in the try: section, because of the hoursdict[hour] = {}
from the previous failure, so changes to hourdict are 
also changes to hoursdict[hour]. 
'MAXT' was the only formula type affected because it was the 
first type for each hour.

Change the except section to:

    except KeyError:
        hourdict = {}
        hoursdict[hour] = hourdict

or:

    except KeyError:
        hoursdict[hour] = {}
        hourdict = hoursdict[hour]

Better yet, change the try/except stuff to:

    if hour not in hoursdict:
        hoursdict[hour] = {}
    hourdict = hoursdict[hour]

Need some better variable names. 


More information about the CentralOH mailing list