Loops Control with Python

W

Wijaya Edward

Can we make loops control in Python?
What I mean is that whether we can control
which loops to exit/skip at the given scope.

For example in Perl we can do something like:

OUT:
foreach my $s1 ( 0 ...100) {

IN:
foreach my $s2 (@array) {

if ($s1 == $s2) {
next OUT;
}
else {
last IN;
}

}
}

How can we implement that construct with Python?

--
Edward WIJAYA
SINGAPORE

------------ Institute For Infocomm Research - Disclaimer -------------
This email is confidential and may be privileged. If you are not the intended recipient, please delete it and notify us immediately. Please do not copy or use it for any purpose, or disclose its contents to any other person. Thank you.
--------------------------------------------------------
 
J

Jon Clements

Wijaya said:
Can we make loops control in Python?
What I mean is that whether we can control
which loops to exit/skip at the given scope.

For example in Perl we can do something like:

OUT:
foreach my $s1 ( 0 ...100) {

IN:
foreach my $s2 (@array) {

if ($s1 == $s2) {
next OUT;
}
else {
last IN;
}

}
}

How can we implement that construct with Python?

Literally.

for si in range(100 + 1):
for s2 in some_array:
if s1 == s2: break

Same thing, but nicer.

for si in range(100 + 1):
if si in some_array:
# Do something here.....

Cheers,

Jon.
 
S

Scott David Daniels

Wijaya said:
Can we make loops control in Python? What I mean is that whether
we can control which loops to exit/skip at the given scope.
For example in Perl we can do something like:
OUT:
foreach my $s1 ( 0 ...100) {
IN:
foreach my $s2 (@array) {
if ($s1 == $s2) {
next OUT;
}
else {
last IN;
}
}
}
How can we implement that construct with Python?

If you are not willing to determine the problem the code is
written to solve, you are doomed to working with "Perl in Python".
While I think Python is a far better language than Perl, I remain
convinced that Perl is a better Perl than Python. Describe an
actual problem, don't simply give an example from another language.

If you don't know about break, continue, and for ... else, go
study the Python language.
 
P

Paddy

Wijaya said:
Can we make loops control in Python?
What I mean is that whether we can control
which loops to exit/skip at the given scope.

For example in Perl we can do something like:

OUT:
foreach my $s1 ( 0 ...100) {

IN:
foreach my $s2 (@array) {

if ($s1 == $s2) {
next OUT;
}
else {
last IN;
}

}
}

How can we implement that construct with Python?

Python does not use Labels. If you want to quit a single loop then look
up the Python break statement. If you want to exit deeply nested
execution then Python has exceptions. this maybe new to a Perl
programmer so please take time to understand Python exceptions.

There follows a function that you can call from an interactive session
to explore one type of use for exceptions that is rather like your use
of Perl labels shown.

==========================
class Outer(Exception):
pass
class Inner(Exception):
pass

def skip_loops(y1 = -1, y2 = -1, y3 = -1):
''' Shows how to skip parts of nested loops in Python'''
try:
for x0 in range(3):
try:
for x1 in range(3):
for x2 in range(3):
if x2 == y2:
raise Inner
if x2 == y3:
break
print (x0,x1,x2)
if x1 == y1:
raise Outer
print (x0,x1)
except Inner:
print "Raised exception Inner"
print (x0,)
except Outer:
print "Raised exception Outer"

==========================
(0, 0, 0)
(0, 0, 1)
(0, 0, 2)
(0, 0)
(0, 1, 0)
(0, 1, 1)
(0, 1, 2)
(0, 1)
(0, 2, 0)
(0, 2, 1)
(0, 2, 2)
Raised exception Outer(0, 0, 0)
(0, 0, 1)
Raised exception Inner
(0,)
(1, 0, 0)
(1, 0, 1)
Raised exception Inner
(1,)
(2, 0, 0)
(2, 0, 1)
Raised exception Inner
(2,)

- Paddy.
P.S. Welcome to Python!
 
H

hg

Paddy said:
Python does not use Labels. If you want to quit a single loop then look
up the Python break statement. If you want to exit deeply nested
execution then Python has exceptions. this maybe new to a Perl
programmer so please take time to understand Python exceptions.

There follows a function that you can call from an interactive session
to explore one type of use for exceptions that is rather like your use
of Perl labels shown.

==========================
class Outer(Exception):
pass
class Inner(Exception):
pass

def skip_loops(y1 = -1, y2 = -1, y3 = -1):
''' Shows how to skip parts of nested loops in Python'''
try:
for x0 in range(3):
try:
for x1 in range(3):
for x2 in range(3):
if x2 == y2:
raise Inner
if x2 == y3:
break
print (x0,x1,x2)
if x1 == y1:
raise Outer
print (x0,x1)
except Inner:
print "Raised exception Inner"
print (x0,)
except Outer:
print "Raised exception Outer"

==========================

(0, 0, 0)
(0, 0, 1)
(0, 0, 2)
(0, 0)
(0, 1, 0)
(0, 1, 1)
(0, 1, 2)
(0, 1)
(0, 2, 0)
(0, 2, 1)
(0, 2, 2)
Raised exception Outer
(0, 0, 0)
(0, 0, 1)
Raised exception Inner
(0,)
(1, 0, 0)
(1, 0, 1)
Raised exception Inner
(1,)
(2, 0, 0)
(2, 0, 1)
Raised exception Inner
(2,)


- Paddy.
P.S. Welcome to Python!
How about a thread on GOTOs ? ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top