Nested WHILE loops in Python -


I am starting with Python and am trying some programs. I have something that creates the following WHILE loop in Python (not accurate).

IDE 2.6.4 & gt; & Gt; & Gt; A = 0> gt; & Gt; & Gt; B = 0> & gt; & Gt; While a & lt; 4: A = A + 1 while B & lt; 4: b = b + 1 print a, b 1 1 1 2 1 3 1 4

I expect the loop to loop through 1,2,3 and 4. And I know I can do it for this kind of loop

  gt; & Gt; & Gt; For one limit (1,5): B in range (1,5): print a, b1 1 1 2 .. .. // Other rows left for short 4 4  

But what's wrong in the WHILE loop? I think I am clarifying something but can not do it out.

Answer: The right WHILE loop ..

  & gt; & Gt; ; & Gt; A = 0> gt; & Gt; & Gt; B = 0> & gt; & Gt; While a & lt; 4: A = A + 1B = 0 while B & L; 4: b = b + 1 print a, b1 1 .. .. .. other rows left for short 4 4  

PS : It has been searched, therefore, but nobody around it. I do not know it can be classified as homework, the actual program was different, the problem is that I have puzzles.

You are not reseting 0 inside your external loop, so b External loop stays at the post value of the first stage - 4 - and the internal loop is never implemented again.

for loops work fine because they do reset their loop control variables correctly; Less-structured while with the loops, such a resetering is in your hands, and you are not doing this.


Comments