Python indentation deters newbies?
beliavsky at 127.0.0.1
Tue Aug 17 02:50:47 CEST 2004
Roy Smith <roy at panix.com> wrote:
>In article <41214271_1 at 127.0.0.1>,
> "beliavsky at aol.com" <beliavsky at 127.0.0.1:7501> wrote:
>> Jorge Godoy <godoy at ieee.org> wrote:
>> >Just an attempt and trying to keep it like your code.
>> Thanks. My code did not correctly illustrate breaking out of more than
>> level of loop. How would the following code be translated to Python?
>Ugh. This is a truly ugly scenario. You've got three nested loops,
>with each inner loop having a break out of both itself and the
>immediately containing loop. Does this correspond to some real-world
>algorithm, or is it just a diabolical scenario invented for the example?
Ok, here is something short but more realistic and IMO not "ugly".
You may want to exit a nested loop when testing if a condition involving
several variables is met, such as searching for a zero of a multivariate
In Python you can print i,j,k and exit() when m == 0, but in a larger program
you may want more control.
! find a Pythagorean triple
n = 5
ido: do i=1,n
ij = i**2 + j**2
m = ij - k**2
if (m == 0) exit ido
end do ido
if (m == 0) then
end program xnest_loop
There is a thread "Labeled loop break/continue" in comp.std.c about adding
similar functionality to C. Fortran has had a big head start over most other
languages, but they may catch up eventually :).
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
More information about the Python-list