Greetings and welcome to part 6 of the Python 3 basics series, in this tutorial we are going to cover functions. The idea of functions is to either take blocks of code that you plan to run a few times, and just consolidate it to one spot, or to also take a block of code and make it slightly more dynamic, to take in unique parameters to output something based on those. We'll get a better on idea on what they do by just using them, however, so let's revisit our game. Let's say we've got our game board:
game = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Before any move is chosen, we want to visualize the board, we don't expect the players to memorize positions, so we'd have:
print(" 0 1 2") for count, row in enumerate(game): print(count, row)
Giving us:
0 1 2 0 [0, 2, 0] 1 [0, 0, 0] 2 [0, 0, 0] >>>
Now, imagine a player inputs a move, and we set that move. For now, let's just set the move with code rather than taking in user input just yet. Say something like:
game[0][1] = 2
Well, after this, we're going want to visualize the map, so we'd call this again:
print(" 0 1 2") for count, row in enumerate(game): print(count, row)
Our code now looks like:
game = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] print(" 0 1 2") for count, row in enumerate(game): print(count, row) game[0][1] = 2 print(" 0 1 2") for count, row in enumerate(game): print(count, row)
So the block of code is repeated twice now already, and obviously we can't keep this up for long! Any time we find that we've got repetition in our code, it should probably be a red flag that you can make it better. In this case, the replication is exact, but, even if there are some slight differences, you should know to probably use a function to handle for it. So, in our case, we can begin defining our function with def
then the name, then parameters inside of parenthesis, followed by a colon:
def game_board():
In this case, we're not going to pass parameters, but we will cover doing this later. Now, for our function, we're just going to pass the code that we've been using:
def game_board(): print(" 0 1 2") for count, row in enumerate(game): print(count, row)
Now, we can call this to run by saying game_board()
. A common mistake is to forget the parenthesis, and call game_board
instead... which just points to the function itself, not for it to actually run!
Finally, for us to display the game board, make a move, then display it again, our code can look like:
game = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] def game_board(): print(" 0 1 2") for count, row in enumerate(game): print(count, row) game_board() game[0][1] = 2 game_board()
Giving us:
0 1 2 0 [0, 0, 0] 1 [0, 0, 0] 2 [0, 0, 0] 0 1 2 0 [0, 2, 0] 1 [0, 0, 0] 2 [0, 0, 0] >>>
Okay, now the moves that we play are also going to be getting repetitive, like, the pattern of moving and then calling game_board(). Thus, we probably want to just pass the next move as a parameter in the function, instead of separate from it. So, we're going to use the next tutorial to talk about function parameters!