# Replacing Connect Four code with a for loop

I have a Connect Four game with a board that I made with a bunch of print functions, which is incredibly inefficient:

    print("   0   1   2   3   4   5   6")
print("   " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " | " + board[0][3] + " | " + board[0][
4] + " | " + board[0][5] + " | " + board[0][6])
print("  ---+---+---+---+---+---+---")
print("   " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " | " + board[1][3] + " | " + board[1][
4] + " | " + board[1][5] + " | " + board[1][6])
print("  ---+---+---+---+---+---+---")
print("   " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " | " + board[2][3] + " | " + board[2][
4] + " | " + board[2][5] + " | " + board[2][6])
print("  ---+---+---+---+---+---+---")
print("   " + board[3][0] + " | " + board[3][1] + " | " + board[3][2] + " | " + board[3][3] + " | " + board[3][
4] + " | " + board[3][5] + " | " + board[3][6])
print("  ---+---+---+---+---+---+---")
print("   " + board[4][0] + " | " + board[4][1] + " | " + board[4][2] + " | " + board[4][3] + " | " + board[4][
4] + " | " + board[4][5] + " | " + board[4][6])
print("  ---+---+---+---+---+---+---")
print("   " + board[5][0] + " | " + board[5][1] + " | " + board[5][2] + " | " + board[5][3] + " | " + board[5][
4] + " | " + board[5][5] + " | " + board[5][6])
print("  ---+---+---+---+---+---+---")
print("   " + board[6][0] + " | " + board[6][1] + " | " + board[6][2] + " | " + board[6][3] + " | " + board[6][
4] + " | " + board[6][5] + " | " + board[6][6])
print()


I want to know how I can replace my board using for loops such that the code will look neater and easier to change. Here is the rest of my code if you need any references.

I think like this:

print("   0   1   2   3   4   5   6")
for row in board:
print(" " + " | ".join(row))
print("  ---+---+---+---+---+---+---")
print()


You could put the whole thing into a single call to print by using "\n".join and more string concatenation, but that would most likely make it a lot less readable.

• Thank you! Do you know how I can create a diagonal check for the winner function? I don't know how to do that...
– Vix
Jan 23 at 19:15
• That’s better for stackoverflow, this site is for reviewing and making suggestions for finished and working code. Jan 23 at 19:26
• I suppose so, but no one's answering my question lopl
– Vix
Jan 23 at 19:34
• You could just use another pair of nested loops since it looks like that’s what you’re using for checking vertical and diagonal lines. But I think you could also set it up so it only checks around the space a piece was placed in. That would be cool to do because it would make it so it would take the same amount of time to check no matter how big the board is. Jan 23 at 20:20