How to break out of two nested for loops?

montanaro at tttech.com montanaro at tttech.com
Wed Jan 23 11:28:28 EST 2002


    Laura> I wonder if people would use exceptions more if they had been
    Laura> named something else.  

Laura, you read my mind.  I think people should dial down their notion of
what an exceptional condition is just a tad.  It's more like the bell that
rings at the end of class instead of the siren that blares from the fire
engine, police car or ambulance, though you can use it for both sorts of
situations.  When that bell rings it doesn't mean the world is going to end
(unless you have a midterm in your next class).  The structure for handling
both sort of situations is similar.  It's just that the actions are somewhat
different:

    def goto_class(what):
        if what == "biology":
            raise YouForgotToStudyForTheTestYouFool
        totaltime = 0
        while 1:
            sleep(60)
            totaltime += 1
            if totaltime == 50:
                raise GoToYourNextNap
            if what in ["history", "french"]:
                pass_a_note_to("Charlotte")

    for nextclass in ["history", "biology", "math", "french"]:
        goto_class(nextclass)

I-have-no-idea-who-Charlotte-is-ly, y'rs,

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)




More information about the Python-list mailing list