Python indentation deters newbies?

Tim Hochberg tim.hochberg at ieee.org
Tue Aug 17 02:14:57 CEST 2004


beliavsky at aol.com 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 one
> level of loop. How would the following code be translated to Python?
> It is silly of course, but real-world situations where you want to exit a
> nested loop are not that rare.
> 
> program xnest_loop
> ! illustrate breaking a nested loop
> integer :: i,j,k,n
> n = 4
> ido: do i=1,n
>    jdo: do j=1,n
>       if (i+j > n) exit ido
>       do k=1,n
>          if (i+j-k < 0) exit jdo
>          print*,i,j,k
>       end do
>    end do jdo
> end do ido
> end program xnest_loop

Hmmm. I've never run into a case where the loop logic is that nasty. 
And,  as it turns out, even the above isn't that nasty if you stare at 
it a minute, it can be coded without break/exit at all. So this is how 
I'd code it <0.5 wink>:

n=4
for i in range(1,n):
     for k in range(1,i+2):
         print i, 1, k

Of course this just dodges your point, but I can't be made to care 
unless you convince me that I'm likely to run into something like this 
in practice.

-tim


> 
> output:
>  1 1 1
>  1 1 2
>  2 1 1
>  2 1 2
>  2 1 3
>  3 1 1
>  3 1 2
>  3 1 3
>  3 1 4
> 
> 
> 
> 
> ----== 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 mailing list