Python 2.4 | 7.3 The for statement
Facundo Batista
facundobatista at gmail.com
Fri Mar 25 12:27:13 EST 2005
On 24 Mar 2005 19:49:38 -0800, brainsucker <jrodrigog at gmail.com> wrote:
> foo = 0
> for item1 in range(10) until foo == 2:
> for item2 in range(10) until foo == 2:
> foo = item1 + item2
> if foo == 2: print "Let's see"
> print foo
In this case, I'll use the following:
try:
for item1 in range(10):
for item2 in range(10):
if item1 + item2 == 2:
print "Let's see"
raise StopIteration
except StopIteration:
pass
print item1 + item2
And I tell you what. Actually I'm very lousy remembering things, and
if I want the loop to stop in a different number, I'll have to change
the code in TWO places. In mine, just one (and this in your codes get
worse with more for levels).
Another approach:
def bar():
for item1 in range(10):
for item2 in range(10):
if item1 + item2 == 2:
print "Let's see"
return item1 + item2
foo = bar()
print foo
. Facundo
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://pyar.decode.com.ar/
More information about the Python-list
mailing list