Can someone explain this Tic-Tac-Toe board code to me?

Viewed 159

I'm just starting to learn Python with "Automate the Boring Stuff with Python" and I was hoping to get help with the code below.

theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ', 'mid-L': ' ', 'mid-M':
' ', 'mid-R': ' ', 'low-L': ' ', 'low-M': ' ', 'low-R': ' '}

def printBoard(board):
    print(board['top-L'] + '|' + board['top-M'] + '|' + board['top-R'])
    print('-+-+-')
    print(board['mid-L'] + '|' + board['mid-M'] + '|' + board['mid-R'])
    print('-+-+-')
    print(board['low-L'] + '|' + board['low-M'] + '|' + board['low-R'])
turn = 'X'
for i in range(9):
    printBoard(theBoard)
    print('Turn for ' + turn + '. Move on which space?')
    move = input()
    theBoard[move] = turn
    if turn == 'X':
        turn = 'O'
    else:
        turn = 'X'
printBoard(theBoard)

When defining the printBoard function, why is there the word board in the parentheses? Shouldn't it be theBoard instead to refer to the dictionary?

In the same block, in the line right under def printBoard(board), why does it say print(board['top-L']...?

How does the program know the 'top-L' key is in myBoard if only the word board precedes it?

4 Answers

board is the name of the function's parameter. theBoard is a variable whose value is used as the argument that initializes the parameter when you call printBoard.

When you call

printBoard(theBoard)

then in the body of printBoard the local variable board refers to the exact same object as theBoard. The way printBoard is written, it's not that it "knows" that board has a key named top-L, but rather it is the caller's responsibility to provide a dict that has the necessary keys.

board in that case is a parameter for the function printBoard. This parameter is a dictionary, meaning it can have multiple pairs of data in it. Through that variable you can define top-L and all the 9 fields of the board.

This is the function does:

enter image description here

By passing the data below to the function:

theBoard = {'top-L': 'X', 'top-M': 'O', 'top-R': 'X', 'mid-L': ' ', 'mid-M':
' ', 'mid-R': ' ', 'low-L': ' ', 'low-M': 'O', 'low-R': ' '}

You are actually representing this:

enter image description here

you need to distinguish function declaration and invocation.

def my_function(my_parameter):
    print(my_parameter)

This is a declaration. This function gets one input parameter and that's parameter name is arbitrary - you could put there anything you like and you can use that name inside the body of a the function Next you can call the function:

a = 15
my_function(a) # will print 15
my_function(50) # will print 50

So here you decide which parameter you would like to pass.

In your case the function call is

printBoard(theBoard)

so the function knows that it's input parameter is your variable theBoard

The board in def printBoard(board) tells the function to accept an argument or parameter when the function is called. This means you could theoretically have different boards printed by passing different dictionaries to the function.

In this case, the printBoard(theBoard) line tells the script to print the theBoard dictionary. From this point, when the function looks for board, it is pointed straight to theBoard.

If you (for some reason) had multiple boards within the game, you could call the function multiple times with printBoard(board1), printBoard(board2) for example, and the function will print whichever board you specify.

If the function was instead coded print(theBoard['top-L']... it would only ever be able to print that board that was hardcoded. In this situation it would work fine, but it's generally good practice to add flexibility into your scripts.

Related