WHILE nested loops in Python

I start with Python and try several programs. I have something like the following WHILE loop construct in Python (not exact).

IDLE 2.6.4      
>>> a=0
>>> b=0
>>> while a < 4:
      a=a+1
      while b < 4:
         b=b+1
         print a, b


1 1
1 2
1 3
1 4

I expect the outer loop to go through 1,2,3 and 4. And I know that I can do this with a FOR loop, like this

>>> for a in range(1,5):
       for b in range(1,5):
           print a,b


1 1
1 2
.. ..
.. .. // Other lines omitted for brevity
4 4

But what happened to the WHILE loop? I guess I'm missing a thing, but I can’t understand.

Answer: The corrected WHILE loop.

>>> a=0
>>> b=0
>>> while a < 4:
    a=a+1
    b=0
    while b<4:
        b=b+1
        print a,b


1 1
.. ..
.. .. // Other lines omitted for brevity
4 4

PS : I was looking for SO, found a few questions , but none of them were close to this. I don’t know if this can be classified as homework, the actual program was different, the problem is what puzzles me.

+5
1

b 0 , b , - 4, .

for , reset; while, , .

+7

All Articles