I am a beginner in python.
The second loop only run for once, the first time only, but when the turn comes to the first loop and when e = e+1 - python skips the second loop!
Why?
The print order only work for once.
items = [['.', '.', '.', '.', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['O', 'O', 'O', 'O', 'O', '.'],
['.', 'O', 'O', 'O', 'O', 'O'],
['O', 'O', 'O', 'O', 'O', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
i=0
e=0
while e < 6 :
while i < 9 : #python run this loop only once, and never come back when e=e+1
print items[i][e]
i=i+1
e=e+1
ikos23
5,50212 gold badges46 silver badges66 bronze badges
-
Debugged this myself, changed the print to just print foo and added a print to the outer while to print bar, foo prints 9 times, bar prints 6 times after that's done. Is this not the expected behaviourL_Church– L_Church2018年05月01日 14:05:13 +00:00Commented May 1, 2018 at 14:05
-
i will try it , coz i haven't use 'print foo' before, i am using python 2.7 , thanks for this information :)Iris– Iris2018年05月02日 15:14:33 +00:00Commented May 2, 2018 at 15:14
-
it doesn't do anything special i just used it so i could see how the loops workedL_Church– L_Church2018年05月02日 15:17:50 +00:00Commented May 2, 2018 at 15:17
1 Answer 1
After the 'i' loop runs once, i will be set to 9 and will stay as 9 until you reset. so you can try to set it to 0 after e = e+1. A useful technique you can try is also printing the values of 'e' and 'i' to see where the loops gone wrong
items = [['.', '.', '.', '.', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['O', 'O', 'O', 'O', 'O', '.'],
['.', 'O', 'O', 'O', 'O', 'O'],
['O', 'O', 'O', 'O', 'O', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
i=0
e=0
while e <6 :
while i <9 :
print items[i][e]
print 'loop: i'+str(i)+'e'+str(e)
i=i+1
e=e+1
i=0
Sign up to request clarification or add additional context in comments.
Comments
lang-py