Python for Absolute Beginners Transcripts
Chapter: Organizing and reusing code with functions
Lecture: Cleaning up Rock Paper Scissors with functions
0:00 Well, this simple version of rock, paper, scissors is cool. We can play it. It has a computer component but there's, again, some shortcomings here
0:07 and boy is it not pretty to look at. It's hard to think about what's going on 'cause you have to read every little detail.
0:14 Like for example, wouldn't it be cool if this could just be all hidden away and that just said Get the rolls?
0:20 And this part down here could all be hidden away and it just says, Get me the winner? Well, let's do that next.
0:26 So what we're going to do is we're going to define some functions. We're going to start up here and define a function that uses the other ones.
0:35 So that allows us to sort of say the high-level things. It allows to say first the program does this basic thing and then it does that basic thing
0:43 and you don't have to worry about the details unless you need to get into them. So let's actually create a function that just does this
0:49 and then we'll use it from the top pieces. And it's really important to have a good name. So what is this doing
0:55 before we say what we're going to call the function? It's printing out the header for the program like the message at the top
0:59 so let's just call this print_header. The way we do this in Python is we use a def keyword. Define function. So we can say def
1:07 and then whatever name we want to make up like show_header like this. It's a pretty good name. The next thing we have to say here
1:13 is what data is passed to it. What information is provided to this function so that it can properly show the header? Well, it takes None.
1:22 So if you wanted to take None you just do open, close parentheses as opposed to a, b like random int but we don't want that so we're going to do this.
1:30 Now again, this is one of these code blocks and if you want it to work and the stuff to be in the function
1:35 you have to do this indent like here inside, okay? So we want these three pieces inside and the quickest, easiest way Is to highlight them all
1:43 and just hit tab and that indents them. Okay, now it's a little unhappy about the spacing. Remember that's just the code style
1:49 that we can fix automatically. So, here we're going to print the header. That's the first thing we're going to do and lets go over here.
1:55 And were going to have this high level thing. We're going to call it main. I'm going to create another function called main
2:01 and the first thing it's going to do. It's going to do show_header. Cool. What is this doing down here? Well, this part here is maybe going to
2:10 play the game. Guess we'll just call this play_game. Def play_game. It doesn't take any information. Maybe we could even pass like
2:19 the names of the player or something like that. Let's call. Let's do that. We'll pass in player_1, and player, player_2.
2:25 And notice these variables are down here like this but instead of having them hard coded here. We want some high level part of our program
2:32 to decide what players there are. So, we'll go over here and do it like this. If we just go down for the moment and indent all of this.
2:41 This is highlighted cause it just wants some spaces here like so. And this. Next thing we're going to do is say play game
2:47 and we can get player_1 and player_2. And notice this has a little warning or error that says You have to tell us what player_1 is.
2:55 I'll call this player and AI. Let's just give them different names and the variable names they have down here. So, we're going to pass for player_1.
3:03 Player, we're going to pass for player_2. AI. Now, if you look at this. This is quite a bit simpler. It says what we're going to do
3:10 in this whole program. Forget what this is. Forget how that works. Get all the details. Let's just do the high level picture
3:17 of what's going on. We're going to say going to show the header and then we're going to play the game. And play game is also complicated
3:23 so we're going to break it in to smaller pieces that are easier to think about as well. Let's go running.
3:28 I think it's going to do something fun and interesting or exactly the same basically. But no that's not what happens. It stopped working.
3:36 Why did it stop working? Well, in Python it turns out that there's no convention to run one of these functions
3:42 so at the very very end you have to make sure you execute the top level piece to get it to run. So we'll run it now. You, what did you roll?
3:50 I rolled paper. Great, computer rolls rock. We take the game with that decisive paper throw. Pretty cool, huh? Now, this is fine.
3:58 But, there's this convention that we use in Python because if we wanted to re-use some of these libraries
4:04 some of this functionality we've written here. This is basically going to prevent that. Cause if we try to load this library
4:10 it's going to just start running the game. Which maybe we don't want. Maybe we want to re-use this bit. Like we use randint.
4:16 So there's this funny weird-looking convention that says, only run this function here if I'm trying to run this file, this program directly.
4:26 Someone else, some other part of the program is trying to use it just define the functions but don't do this part.
4:32 And the convention is built into PyCharm. So if I type main, notice there is one up at the top that's the function I wrote.
4:39 But this one is something else. This is what's called a live template. PyCharm will just write that for us. And it says Here's the convention.
4:47 There's this thing called __name__ and if it gets set by now which when you run it directly but, is not done when you don't run it directly.
4:54 So this is a way to test. Only run it if you're trying to use it as a program. Finally, let's run it one more time. Hey, it works. Rock again.
5:03 They roll scissors. Game is over, we crush them with a rock. Awesome. So we've done a little bit of organization, but notice this 19 to 72.
5:12 There's a lot of stuff going on in here. We're going to make this a lot cleaner and nicer and break it up into pieces as well.
5:18 But we're off to a good start. Defining these functions which we use a the def key word. Give them a name and they either take no arguments.
5:26 Or we define them, give them a name and they take one or more arguments. There's actually a lot flexibility what you can specify they take
5:33 what is optional what is required and so on. We don't need to go too much into it but what we have here is
5:39 probably a good enough example for most functions you need to write.