1
votes

Code:

NUM_SQUARES = 9
EMPTY = " "

def new_board():
    board = []
    for square in range(NUM_SQUARES):
        board.append(EMPTY)

    return board

Background:

I was reading this piece of code for the game 'Tic-Tac-Toe'. I don't understand the for loop in the function new_board().

My Understanding:

So in the first part of the code, I understand that there are two constants for: the number of squares on the board, an empty square on the board. In the function, an empty list is created where empty strings would be added to represent blank squares.

I understand that in range(NUM_SQUARES): means that it will iterate the code below it 9 times. Therefore, it will add nine empty strings as items in the list.

What I don't get:

1) What is the square variable assigned to?

2) What is the purpose of needing this variable square?

3) Why do we commonly say i in for i in range()? (in general)

2

2 Answers

1
votes

1) As you mentioned, in range(NUM_SQUARES) means that the commands within the loop will be executed NUM_SQUARES times. The first time it is executed, square will be equal to 0. Then 1, 2 etc. It's value is not used so you could have written for i in range(NUM_SQUARES). Often when you don't need the loop index you would write for _ in range(NUM_SQUARES).

2) The variable is not explicitly needed, but you just need to specify some variable. That's just how for loops work in python.

3) i, j, k is often used for integers in programming, math, physics etc.. I believe that is just the reason why we choose 'i' instead of something else.

0
votes

This is an interesting mechanism for creating a list to be used for housing the board state of a tic tac toe game. I'll start off by answering your third question, and build from there.

3) i is short for 'index' when used in the classical for-loop structure. In python, index-based loops are typically used when you need to access the actual index itself (e.g. you have something where you need to perform an action on array_member[i]). You could write the loop as for index in range() if that is more explicit/makes better sense; it doesn't matter to the language what name you choose for the index variable.

2) Building on what we covered in #3, the purpose of the variable square in this context, is to act as the index for the list board over which we are iterating. The naming convention of square is more commonly seen when using a for-each loop, where presumably you would already have a list of squares that you were iterating over:

for square in list_of_squares:
    if square == 'X':
        # Do something, etc.

1) Based on all of this, the square variable starts off with 0 assigned to it. After the first iteration of the loop, it becomes 1, then 2, etc... until it reaches the limit specified by the range (in this case NUM_SQARES, or 9). It's worth noting that the range function is non-inclusive of the boundary specified, so this loop will execute from 0 to 8, which yields a total of 9 'squares' within the array.