[Tutor] nested loops
Steven D'Aprano
steve at pearwood.info
Mon Feb 7 22:27:01 CET 2011
Alan Gauld wrote:
> "Wayne Werner" <waynejwerner at gmail.com> wrote
> You probably want to add a sentinel to break out of the outer
> loops too:
I don't think you want to break out of *any* of the loops. Otherwise you
will skip testing combinations. In your example, the first time you set
highscore and alignment, you break out of all three loops and only test
the first triple x,y,z.
> found = False
>> highscore = 0
>> alignment = somealignment
>> for x in something and not found:
>> for y in somethingelse and not found:
>> for z in evenmoresomething:
>> if x+y+z > highscore:
>> highscore = x+y+z
>> alignment = newalignment
> found = True
> break
That's the equivalent of a return in the innermost loop. If you're
looking for the *first* matching highscore, that's fine, but not if you
want the biggest.
--
Steven
More information about the Tutor
mailing list