Python for Absolute Beginners Transcripts
Chapter: Organizing and reusing code with functions
Lecture: Functions for play-game

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Up here we're able to give a high-level overview
0:02 of what's happening in this program or our main
0:05 but our play game is still kind of
0:06 like the rest of everything.
0:08 It's a bit of a mess.
0:09 So let's go and work on making this cleaner.
0:11 Now, this part right here, this whole section is going
0:15 to give us the name of player_1, right
0:19 or the roll of player_1, sorry.
0:20 So let's go and actually make that something much simpler.
0:24 Instead of looking at all of this stuff here
0:26 what we want to do is we want to create a function
0:29 that would just say get_roll
0:31 and it's going to ask player_1, What is your roll?
0:34 Now, there's a couple of things
0:35 that we're going to have to do.
0:37 Notice that it's working with rolls.
0:40 If I click here, you see it highlight.
0:41 Notice that it kind of colors all the places it's used.
0:44 But it's working with rolls.
0:45 It's also working with player_1, which is passed in here.
0:49 So in order for this to be a function
0:52 it has to be provided those two pieces of data
0:54 all the rolls and the name of player_1
0:57 because those are going to be coming from other places.
1:00 Let's go down here and do that
1:02 a def get_roll or get throw.
1:04 It's have to be player name
1:06 and it's going to have to have all the possible rolls here.
1:11 And then we're going to put something here.
1:12 If you don't know what to put yet
1:13 the thing to write in Python is pass.
1:15 That means it does nothing for the moment.
1:18 Let's go over here, and let's just grab that.
1:22 Want to say roll one equals get_roll.
1:25 We need to pass player name.
1:27 So that's player_1.
1:28 And all the rolls, we're going to pass all the rolls.
1:31 Notice we can't define the rolls down in that function
1:34 'cause this part also needs them, right?
1:36 Now, there's a squiggly here saying
1:38 You're not quite done with your function.
1:39 We know that.
1:40 So I just copy that bit down here.
1:43 I'm going to paste that like so.
1:45 Now, I change the name to make it sound more general
1:47 'cause this will work for any player
1:49 if we want to have competitive players against each other.
1:53 We're going to do that.
1:55 Again, for the same reason, this is not roll one.
1:58 This is just the roll.
1:59 So what we need to do is everywhere we see highlighted
2:02 down here, this needs to be renamed from roll one to roll.
2:06 Now, we could do it manually.
2:07 It's only four or five spots here.
2:09 It's not a big deal.
2:10 But you want to make sure you don't miss something.
2:13 You want to make sure you get it exactly right.
2:14 So PyCharm has this cool feature
2:16 and it's a general idea in programming
2:18 but it's built into PyCharm
2:20 to do what's called refactoring
2:21 and that is to change your code in certain structured ways
2:25 without making it mean anything different.
2:28 So watch this.
2:29 If I right-click here and I say refactor, rename
2:32 if I go over here and I type take that away
2:33 notice it changes it everywhere.
2:35 And I could have anything I want
2:36 and it makes sure all the code
2:38 that was trying to use roll one is going to be
2:40 continue to be consistent with that.
2:43 Cool, huh?
2:44 Okay, so and to make it stop, you just hit Enter.
2:46 Now, that's a pretty good start.
2:48 However, we've got this information.
2:50 How do we give it back?
2:52 How do we go to whoever's trying to use this function
2:54 and say, Here's the roll they selected?
2:56 Now, that's the last step down here.
2:57 We have to say return.
2:59 This is what value did the function create?
3:02 What does it provide back as its functionality?
3:05 We're going to pass back this roll.
3:07 If we go back up here, this little squiggly went away
3:10 because what it was saying was this get_roll
3:12 that you're calling, it doesn't return any information.
3:14 It doesn't tell you anything
3:16 and yet you're trying to set roll one equal
3:18 to a function that gives you nothing.
3:19 But now it does give back something
3:21 so PyCharm is again happy.
3:23 So here we're getting our two rolls.
3:25 This function is really interesting
3:26 'cause it takes two parameters like we saw before
3:29 but it also has a return value that we can use.
3:32 And here in this context
3:34 this is meaningful as roll one, not just a random roll.
3:37 There is one other thing here.
3:39 Notice here where we say, There's a problem.
3:42 Sorry, you can't play that.
3:43 Let's tell, instead of returning whatever that was
3:46 we'll return none.
3:47 So this is a way we can say back to who tried to call it
3:50 hey, we tried to get a roll from the player
3:53 but they gave us something nonsensical
3:54 so it doesn't mean anything for us.
3:56 We're going to just say we got nothing.
3:58 And down here, we can do a quick test.
4:00 We'll say if not roll one, print, can't play that, exiting.
4:06 Here we can just get out of this game early
4:10 by just saying return.
4:11 There's no data, so we don't say return roll or anything.
4:14 We just say get out.
4:16 And this will make it stop.
4:17 So this will actually solve one of the problems
4:19 that we had before.
4:20 Go ahead and run it, see how it's working.
4:22 Whew, okay, so you, what is your roll?
4:24 Gosh, that's kind of weird to say.
4:27 This is the function that we just wrote.
4:28 That is this get_roll on line 22.
4:30 So let's say paper.
4:32 It passed back paper here to roll one
4:34 it checked that that was okay, and it said player_1 rolls
4:38 player_1 is you, you roll paper.
4:41 Awesome, right, so it looks like that worked.
4:43 And then it comes back and it does a test
4:45 and you takes the game.
4:46 Yeah, this is a little weird, this name thing.
4:48 Cool, though, right?
4:49 So what we've done is we've taken something
4:51 that was kind of complicated, like four or five lines
4:54 and it also had this problem
4:55 about how do we get out of the game early?
4:57 We condensed it down to just this idea of get the roll.
5:00 All the validation is checking is there
5:03 talking to the users there, all that
5:06 and we don't have to think about it.
5:07 Now when we think about playing the game
5:08 all we got to know is you get the roll from the player
5:11 you randomly get one from the computer.
5:13 Boom, off it goes.
5:14 So getting this input from the user is much, much nicer.
5:18 And down here if you need the details, you can go see them.
5:21 But if you don't, you can just collapse it away
5:23 and think, okay, get_roll.
5:24 I give 'em a player name.
5:25 I get a roll.
5:26 Give 'em all the rolls.
5:27 I get one, a validated roll back, or I get no roll.
5:30 And that's why these functions are often referred to
5:32 as black boxes.
5:34 You know what goes in.
5:35 You know what comes out. And if you want, you don't have to go over here and peek
5:38 at what's actually happening.
5:39 When done right, you can hide it away and say
5:42 I just know what get_roll does.
5:43 It's going to get me a valid roll, or it's going to get me none.
5:45 I think that's pretty cool
5:47 and it's a really nice way to think about playing this game.
5:49 So this part is much, much simpler.
5:53 We still got to deal with the winner bit
5:54 but we're making good progress by simplifying our play game.