Rudeboii ezuronye Rudeboii ezuronye - 6 days ago 5
Python Question

Trying to increment using for nested loops

I'm trying to increment using for nested loops but not getting the desire result. The purpose for this is to check for diagonal matches for the game connect 4 using a 7x6 grid. Here is my code and results

for i in range(6):
for j in range(6):
print("[{}][{}]".format(i,j))
i+=1

#This is the output i am trying to get.
[0][0]
[1][1]
[2][2]
[3][3]
[4][4]
[5][5]
[1][0]
[2][1]
[3][2]
[4][3]
[5][4]
[2][0]
[3][1]
[4][2]
[5][3]

#But this is what i am getting
[0][0]
[1][1]
[2][2]
[3][3]
[4][4]
[5][5]
[1][0]
[2][1]
[3][2]
[4][3]
[5][4]
[6][5]
[2][0]
[3][1]
[4][2]
[5][3]
[6][4]
[7][5]
[3][0]
[4][1]
[5][2]
[6][3]
[7][4]
[8][5]
[4][0]
[5][1]
[6][2]
[7][3]
[8][4]
[9][5]
[5][0]
[6][1]
[7][2]
[8][3]
[9][4]
[10][5]

Answer

This code returns your desired results without truncating the data.

for i in range(6):
   for j in range(6-i):
      print("[{}][{}]".format(i,j))
      i += 1

For your exact output:

for i in range(6):
   check = False
   for j in range(6-i):
      print("[{}][{}]".format(i,j))
      if i == 5 and j == 3:
         check = True
         break
      i += 1
   if check:
      break

Output:

[0][0]
[1][1]
[2][2]
[3][3]
[4][4]
[5][5]
[1][0]
[2][1]
[3][2]
[4][3]
[5][4]
[2][0]
[3][1]
[4][2]
[5][3]
Comments