Built-in Functions - Learning to Program with Python 3 (basics)




Hello and welcome to part 4 of the Python 3 basics tutorial series in this video we're going to continue to build out our TicTacToe game. Where we left off, we had code that could visualize at least the starting game board for us:

game = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],]

for row in game:
    print(row)

Output:

[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
>>> 

Now that we have this, the next step in a game of TicTacToe would be...someone wants to play! So, now, we have to think of some way for players to input where they want to play. Being that this game is text-based and has no real user interface, we can't just let players "click" where they want to play. Instead, we can utilize the fact that this is a grid, and let players input the position on the grid, in a sort of x,y fashion.

So what I am thinking of is something like:

   1  2  3
1 [0, 0, 0]
2 [0, 0, 0]
3 [0, 0, 0]

Now, if we ask the user something like "Where would you like to play? (row, column)," the user could input something like 1,2. Where would this be? Row 1, column 2. So that might be:

   1  2  3
1 [0, X, 0]
2 [0, 0, 0]
3 [0, 0, 0]

Okay. So that's my idea of how I want to do it, now we just need to program it! Okay, so this breaks down again. Remember a moment ago how the game of TicTacToe was apparently only four steps?

  1. 1. Visualize the game somehow
  2. 2. Allow players to enter moves
  3. 3. Make sure moves are valid and handle if not.
  4. 4. Determine if there's a winner.

Well, it turns out.... we're still on step #1 and now we've got some sub steps! A lot of programming is just like this. You come up with the macro steps, then you start working on the macro steps and these have sub-steps that you will need to break down in the same way.

How might we break these ones down? Well, we're wanting to add some numbers to the top of the game board, and to the side. The top numbers are super simple. We can just...print them out. Let's do that first!

game = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],]

print(" 0  1  2")
for row in game:
    print(row)
 0  1  2
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
>>> 

Note I started with 0, rather than 1. In programming, "counting" starts at 0. We can start with 1 for the player's visualization, but later we'd need to subtract one for their inputs. This is totally fine if you want to do that, but I'll do it this way to keep it simple for now. Okay, what about the right-hand side?

Here's a perfect example of a programmer's ability to do just about anything with basic tools, even when there are more advanced tools for this exact purpose. To begin, we could create some sort of simple counter.

Before our for loop, we could add in count = 0. Then, in the loop, we can print(count, row). Then, right under that, do count += 1. Whooooaa there what's this += stuff?!?!

Something like count += 1 is the same as doing count = count+1. It's just a quick way to add things. You could do count += 5 for example too, to add 5 every loop. Finally, you can also do all of the other operations like this like *= or -= or /=. Okay, back on track here's our code:

game = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],]

print(" 0  1  2")
count = 0
for row in game:
    print(count, row)
    count += 1

Output:

 0  1  2
0 [0, 0, 0]
1 [0, 0, 0]
2 [0, 0, 0]
>>> 

Okay, our top row got out of alignment because we added more text to the rows below it. No big deal, add 2 more spaces:

game = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],]

print("   0  1  2")
count = 0
for row in game:
    print(count, row)
    count += 1
   0  1  2
0 [0, 0, 0]
1 [0, 0, 0]
2 [0, 0, 0]
>>> 

Nice, looking good! We solved the task just fine. This code is more than fast enough and it's logic is sound. ...but there is a better way. This was is with enumerate. Enumerate() is another built-in function with Python, and it's one of the more "fancy" tools that is indeed nice to have and know, but you don't actually need it to get the job done. It does become exceptionally more useful when you intend to iterate over a few iterables together, though there are also many ways (including with fancy tools) to do this too! One thing to learn quickly about programming is that there are many ways to do something, and people get all tribal about *their* way. The bottom line is: make stuff! It's never going to be perfect. Pretty much any code you ever write will have bugs, it will not be as fast as it could be. If you get hung up on this, you'll never actually produce something, and that's a bummer!

Anyway, enumerate, let's check it out, shall we? All this does is return the item and its index value as we iterate. This allows us to remove 2 lines of code and just modify one to achieve the same objective. Our for loop now is:

for count, row in enumerate(game):
    print(count, row)

That's it! Since enumerate is a function, it has the ability to return things. In this case, the enumerate function is returning the index and item. We can do what's called unpack here and unpack these into tuple form like we've done above with count, row in the for loop. Full code now:

game = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],]

print("   0  1  2")

for count, row in enumerate(game):
    print(count, row)

Result:

   0  1  2
0 [0, 0, 0]
1 [0, 0, 0]
2 [0, 0, 0]
>>> 

Very cool! We might still be a bit fuzzy on what a function is, and that's okay. We will cover functions more in depth in part 6. For now though, we've got a game board. It looks like it's ready for a player to play on it. if we said row 0 column 2, we'd expect that to be:

   0  1  2
0 [0, 0, X]
1 [0, 0, 0]
2 [0, 0, 0]

So now, how do we actually mark that, should we receive that intention from a user? Let's talk about that in the next tutorial!

The next tutorial:





  • Introduction to Python 3 (basics) - Learning to Program with Python 3
  • Tuples, Strings, Loops - Learning to Program with Python 3 (basics)
  • Lists and Tic Tac Toe Game - Learning to Program with Python 3 (basics)
  • Built-in Functions - Learning to Program with Python 3 (basics)
  • Indexes and Slices - Learning to Program with Python 3 (basics)
  • Functions - Learning to Program with Python 3 (basics)
  • Function Parameters and Typing - Learning to Program with Python 3 (basics)
  • Mutability Revisited - Learning to Program with Python 3 (basics)
  • Error Handling - Learning to Program with Python 3 (basics)
  • Calculating Horizontal Winner (tic tac toe) - Learning to Program with Python 3 (basics)
  • Vertical Winners - Learning to Program with Python 3 (basics)
  • Diagonal Winners - Learning to Program with Python 3 (basics)
  • Bringing Things Together - Learning to Program with Python 3 (basics)
  • Wrapping up Tic Tac Toe - Learning to Program with Python 3 (basics)
  • Conclusion - Learning to Program with Python 3 (basics)