Trying to understand nested loops

dn PythonList at DancesWithMice.info
Fri Aug 5 19:15:26 EDT 2022


On 06/08/2022 10.50, Dan Stromberg wrote:
> On Fri, Aug 5, 2022 at 12:35 AM <ojomooluwatolami675 at gmail.com> wrote:
...

> Of if you don't have (or want) a debugger, you could change it to:
> 
> var = 0
> for i in range(3):
>   print('i is', i)
>   for j in range(-2,-7,-2):
>     print('j is', j)
>     var += 1
>     print(var)
> 
> And note that 3 times 3 is 9.

Combining the above advice and illustration, with several more recent
comments about indentation revealing structure, should the above be
amended to:

indent = "    "  # your choice of how many spaces/tabs
...
  print( "i is", i )
...
    print( indent + "j is", j )
...
      print( indent + indent, var )  # add sep="" if pernickety
...

Thus, the output matches the code and each complements the other's view
of structure!

-- 
Regards,
=dn


More information about the Python-list mailing list