Python indentation deters newbies?

Jorge Godoy godoy at ieee.org
Tue Aug 17 00:29:01 CEST 2004


beliavsky at aol.com writes:

> ido: do i=1,n
>    ! some code
>    if (foo) exit ido ! exit outer loop
>    jdo: do j=1,n
>       ! some code
>       if (boo) exit jdo ! exit middle loop
>       do k=1,n
>          ! some code
>          if (goo) exit ! exit inner loop
>       end do
>    end do jdo
> end do ido

Just an attempt and trying to keep it like your code. 


>>> n = 10          # Just needed to run this...
>>> for i in xrange(1, n):
...     print 'i =', i      # some code...
...     if (i == 5): break  # exit outer loop
...     for j in xrange(1, n):
...         print 'j =', j       # some code...
...         if (j == 3): break   # exit middle loop
...         for k in xrange(1, n):
...             print 'k =', k        # some code...
...             if (k == 2): break    # exit inner loop
i = 1
j = 1
k = 1
k = 2
j = 2
k = 1
k = 2
j = 3
i = 2
j = 1
k = 1
k = 2
j = 2
k = 1
k = 2
j = 3
i = 3
j = 1
k = 1
k = 2
j = 2
k = 1
k = 2
j = 3
i = 4
j = 1
k = 1
k = 2
j = 2
k = 1
k = 2
j = 3
i = 5
>>>


By using "<=" one could have one more iteraction, if this is what is
done with your original code.


Be seeing you,
-- 
Godoy.     <godoy at ieee.org>



More information about the Python-list mailing list