Python for Entrepreneurs Transcripts
Chapter: Python language refresher
Lecture: Demo: Memory game - Structuring the program
Login or purchase this course to watch this video and the rest of the course contents.
0:01 So let's take a few moments and spend some time getting you familiar with Python, maybe you've seen it a little bit but it's been a while
0:08 or maybe you are fairly new and just seeing it in action will teach you much of what you need to follow along,
0:14 and of course, we have the detailed reference section in the course if you want to look at any specific topic or concept.
0:21 What we are going to build in our demo section is a memory game. It will show colors to the user
0:26 and the user will have to recall the colors in the correct order. It's going to be very simple but at the same time
0:31 it's actually going to cover a decent amount of what you need to know to get started outside of the web frameworks, which we'll cover separately.
0:38 So, here we are in PyCharm, and I've created a new empty project based on Python 3, but that's all I've done.
0:44 To get started, we are going to need to create at least one file, let's go and create a couple of files here, one of them is going to be our game
0:52 and this is going to be the actual objects and data structures in our game.
0:56 And the other one is going to be the plain of the game, so we'll call this one "program". Let's focus on the game first.
1:04 Like I said, the game is going to keep a history of colors shown to the user and then we'll go back and ask them "what colors did you see?",
1:10 and of course hide the colors and say "what colors did you see?", and they have to recall them in the correct order.
1:15 So let's create a class called Game, the way we do that is we use the class keyword and say the name and the type
1:22 if we had any subtypes, we could derive it from here, but we are not going to do that, and we are going to go over here
1:27 and we are going to define an initializer. So, a __init__ and PyCharm will write most of that for us. In here, we need to keep track of a few things,
1:36 we want to keep track of the history and so on, but let me just write "pass" for just a minute.
1:41 What I want to do is I actually want to sketch out how we are going to use this gameplay,
1:45 and I want to show you how PyCharm makes this incredibly easy to do. So we are going to have things like "add a move", or "add a color",
1:53 "show the current configuration" or the level if we are going to call it that, maybe first level, second level, and number of colors they have.
2:00 And then we want to test the users, so we want our game have those operations.
2:03 I could just go ahead and just say "def" and I want to create a method here, so I create a method by saying "def", let's call it show_level(),
2:11 and when I hit open parenthesis you'll see that PyCharm add self, which is required as the first parameter for Python classes,
2:17 and we are not going to really need anything else here. So, I could do some stuff that we are going to work with here,
2:21 some variables we want to set, but I want to show you that we can actually sketch out the flow and have PyCharm fill in the details for us.
2:27 We are going to come over here and we are going to have a main() method, this is not a Python convention, this is just a Michael convention,
2:34 and what I want to do is I want to work with our game in other modules, so we'll say "import game" and then we could say "game.game"
2:42 or perhaps a better way would be "from game import game" like this. So, that's going to be our game object down here
2:49 so we'll say "g = Game()" to get started, and we have to think well, how is this going to be played? We probably want to go around in a loop
2:56 as long as the person can remember all the steps, we want to do a few things each time through the loop.
3:02 So we'll do a "while" loop, we'll say "while True", because we just want to keep going around and around as long as the person has won.
3:10 So the first thing we want to do is say "game", we are going to say add move.
3:14 Now notice, this method does not exist, and PyCharm is highlighting this saying: "there is a problem here, this doesn't exist."
3:20 But, we can come over here and hit Alt+Enter and it will actually add that method to class for us.
3:25 So we can go over here and say "we are going to add a move" and then we want to say "game.show_level()", that's the next step we want to do,
3:31 and then, we want to test the user, so we'll do a conditional here, we'll say "if", we want to see whatever we want to do to test them,
3:38 we'll ask them to enter the colors, maybe all in one line, or one entry per line if we're at level 5, there might be 5 entries they have to put,
3:46 However we test them, we want to have the game do that test here, we want to check,
3:50 if they don't get it right, we want to stop the game and say "sorry, you lose"; if they do, then we want to just keep going through loop,
3:56 so we'll say "if not" - the negative of testing, something like this,
4:00 we'll say test_user, a player, let's call them player, this is kind of more fun to call them player,
4:05 so we'll say test_player, if that's not the case, what are we going to do, well, let's tell them they lost, so we can print out the message
4:11 and we can break out of this loop. And down here, we will print something like this, "Game over". So, let's look at this for a moment,
4:18 we are going to create a game, which comes out of our module here, and while basically forever, until we decide it's time to break out,
4:25 we are going to add a move, we are going to show the level to the user, and then we are going to take it away
4:30 and then we'll test the user and if they get it right, we are going to add yet another move, show the level, which is now more complex and so on.
4:36 This will not actually run, if I run this program here, it will just basically define this method,
4:41 so in Python, we have a common convention that lets you say "if you are running this program,
4:48 instead of just defining the method you can actually run it", and the way we do that is we can use a shortcut
4:54 and the convention is this __name__ that is in the module whatever module's been run,
4:59 if it's being imported, the name will be the name of the module like "program" or "game",
5:04 but if it's being executed as a target, it will be __main__ and we can say "OK, this is the time where we want to call this method we just wrote."
5:11 I can see that PyCharm says we don't yet have this method, let's go and write that as well and it's going to need to return True or False.
5:17 Ok, so we now have this structure for our game, what we need to do is actually go implement the game class over here,
5:24 what does it mean to show the level, what does it mean to add a move, things like that.