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