Ask coding questions

← Back to all posts
out of range error in Connect 4
reddevils1979 (0)

Trying to make a Connect 4 game, but I am having trouble bracketing the board. Everything here works fine if I make it without the boarders (like a 6x7 Tic Tac Toe), but when I try to add the edges on the sides, top and bottom I start getting my "index is out of range". I figure my mistake has something to do with player moves going on odd rows & columns now instead of even rows & columns (like it does on TicTacToe), but I don't know how to fix it.

Answered by Coder100 (18145) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (18145)

anyways, you forgot that arrays start at 0, so you have to actually subtract 2 making it:

for row in range(11):
  ...
  for column in range(13):

because (15 + 1) / 2 = 8 and that is in fact bigger than what your array has.

reddevils1979 (0)

@Coder100 doesn't a column range of 15 mean they're numbered 0-14? I was trying to place the player input in only the odd number columns/rows with the "if row%2 != 0". I thought that would limit it to (13 + 1) / 2 = 7 for columns and (11 + 1) / 2 = 6 for rows. I could totally be doing this the wrong way, this was just my line of thought.

reddevils1979 (0)

@Coder100 you're right and it does let me put the input in now. I just want to know the hole in my thought process if you see it.

Coder100 (18145)

well
you see
at 15, we have: (15 + 1) / 2 = 8, and the highest you can index is 8 - 1 = 7 because the array starts at 0. @reddevils1979

reddevils1979 (0)

@Coder100 I did have to go back to 13 & 15, but dropping the +1 got everything to work right. Thanks for the help!

Coder100 (18145)

You have 7 columns with 6 rows.
Where did you get the numbers of 13 and 15?

reddevils1979 (0)

@Coder100 thanks for reaching out! 7columns & 6rows where input can be entered. There are still 7 rows with "---------" reaching across and 8 columns with "|".