0

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    
3
  • 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 behaviour Commented 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 :) Commented May 2, 2018 at 15:14
  • it doesn't do anything special i just used it so i could see how the loops worked Commented May 2, 2018 at 15:17

1 Answer 1

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.