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

Talk Python's Mastodon Michael Kennedy's Mastodon