how to make this situation return this result?

Peter Otten __peter__ at web.de
Sat Jul 1 06:00:04 EDT 2017


Ho Yeung Lee wrote:

> expect result as this first case
> 
> ii = 0
> jj = 0
> for ii in range(0,3):
>     for jj in range(0,3):
>         if ii < jj:
>             print (ii, jj)
> 
> 
> but below is different
> as sometimes the situation is not range(0,3), but it a a list of tuple
> 
> iiii = 0

> for ii in range(0,3):
      # jj starts with 0 on invocation of the inner loop
      # to get that with an independent counter you have to
      #initialise it explicitly:
      jjjj = 0
>     for jj in range(0,3):
>         if iiii < jjjj:
>             print (iiii, jjjj)
>         jjjj = jjjj + 1
>     iiii = iiii + 1
> 
> how to make this situation return result like the first case?

Because you don't reset jjjj the condition iiii < jjjj is always True the 
second and third time the inner loop is executed.




More information about the Python-list mailing list