I am writing a tic-tac-toe program and whenever my game gets to here it generates the following error:

# Display board function
def display_board(board):
	"""Display game board on screen."""
	print "\n\t", board[0], "|", board[1], "|", board[2]
	print "\t", "------"
	print "\t", board[3], "|", board[4], "|", board[5]
	print "\t", "------"
	print "\t", board[6], "|", board[7], "|", board[8]

print "\n\t", board[0], "|", board[1], "|", board[2]
IndexError: list index out of range


If this isn't enough code to help with the error I can post more, just don't want to take up pages with unnecessary code.

Thanks in advance

print "\n\t", board[0], "|", board[1], "|", board[2]
IndexError: list index out of range

Add a
print len(board)
as the first statement in the function to see if your list contains 9 items (and of course it doesn't, but this will tell you how many items it does contain).

Ahh, thank you. It appears there is only 1 instead of nine for some reason? Must have messed up somewhere, but thank you for helping me to the next step in a solution!

# New board function
def new_board():
	"""Create new game board."""
	board = []
	for square in range(NUM_SQUARES):
		board.append(EMPTY)
		return board

Where
NUM_SQUARES = 9
EMPTY = " "


Any idea why this is making a list size 1? I'm working an example out of a book here and it says that this function creates a new board(list) with nine elements set to empty and returns it. I thought that with the for loop it would create nine, and I really don't see where it gets a 1 from. I even tried replacing the variable with a 9 but no luck, still says 1.

See if adding this print statement gives you any ideas.

# New board function
def new_board():
    """Create new game board."""
    board = []
    for square in range(NUM_SQUARES):
        print "adding square #", square
        board.append(EMPTY)
        return board
commented: Very very helpful, helped me work out a bug I had been having lots of trouble with! +0
commented: nice help +10

Thank you thank you thank you! Turns out that was the last error in my program! Works great now!

Here is the correct code now:

# New board function
def new_board():
	"""Create new game board."""
	board = []
	for square in range(NUM_SQUARES):
		board.append(EMPTY)
	return board

I had the extra indent and apparently the return was causing the loop to end! I really appreciate the help, and even more so how you helped me. I feel much better knowing I figured it out instead of someone just giving me the answer.

Two more things. You do not pass the variables NUM_SQUARES or EMPTY to the function. If you run/call this function from another program file you will get an "undefined variable" error. Second, I come from another time and another programming style and so code based on the indents rather than chronological order. I will not have the indent problem because the function would be coded as a for statement and a return value. The code under the for statement would be added last. Don't know if this will help or not, but the idea is to vary your style as you go along and improve incrementally .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.