Python for Absolute Beginners Transcripts
Chapter: Organizing and reusing code with functions
Lecture: A function for winning

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, the first part of our play game is much, much nicer. This line 22, get the roll validated from the player. And it gets us a chance
0:08 to get out of here if that doesn't work. This other part of checking for a winner makes up a ton of stuff over here. So all the way, this reporting
0:18 this is not really getting the winner this is more about reporting who won. But from here all the way to there that blue bit, that is the code
0:28 along with a comment that we wrote, to get the winner. So what we've done so far, is we've gone and said Okay, well let's make a function.
0:34 I'm going to go type def, a thing I want to call it. Figure out parameters go in there, what gets returned. And it's very manual.
0:40 And you need to be able to do that. You need to do enough practice so that that makes a lot of sense for you.
0:46 But once you get used to it, you can leverage tools like PyCharm, to make this much more error free. Okay, down here, remember, in this section
0:54 I said, I want to rename roll. So I'm going to use this refactor idea to make sure that PyCharm can verify every bit where we're using roll, roll one.
1:03 If we want to rename that we do that in a consistent and valid way. But with functions, we can do that even better. So watch this.
1:09 So I want to take all of this and that winner and test. So let's look through that process again. So it's going to take roll one and roll two
1:16 it needs that information. It needs to know player_1 and player_2. And then it has to give back the winner so the winner can be used here.
1:24 So watch this. Don't have to use this tool but it's super cool to help you when you're getting started or when you're a professional.
1:31 It'll be good for a long time. So extract method this means take all this code I've highlighted and create a function.
1:38 Give it everything it needs to get started and return what it needs to return. Beware, this doesn't always work. Sometimes things are too complicated
1:45 to make sense to do this automatically. So, let's see what we get. So it says we're going to have a extract. Extract method is going to need a name
1:53 this is the name we want to call the function. We'll say, check_for_winning_throw. And notice, it says it takes player_1, player_2
1:59 roll one, and roll two as you would expect there. And let's make this a little taller. It's kind of weird, the way it's getting squooshed.
2:07 Well, whatever. It doesn't want to resize. So then notice the output variable is winner. And if I hit okay, watch this.
2:13 Boom, all that stuff, all those complicated lines are reduced down to this. check_for_winning_throw.
2:20 Give it player_1, player_2, and roll one, and roll two. And then it's going to come back and do the same test with the data, here, that is returned.
2:27 And then down here this is the thing that was created for us. Right? All this code down here was written by PyCharm when we said extract method.
2:35 So that's pretty cool, right? We could leave this comment up here. Probably this check for winner test for winner, is not necessary
2:41 because check for winning throw already tells us. We could leave these comments here if we think they're going to be helpful probably they are for now.
2:48 So let's just leave that. Now, we've changed our code in a way that's way cleaner like hide that away, hide that away.
2:54 Now we can look at this and we can say Okay, what does our program do? Choose the header, comes up with the two players
3:00 and it plays the game. And we could actually do this as well. We come down here and just write in place, you and computer. Like that.
3:08 And we don't even have to explicitly do that. So we're going to show the header and play the game. Play game is made up of getting the rules
3:15 and getting the rule validated from the player. If you can't play, you can't play. We have this little print out, so we know what's happening.
3:21 And then we check for a winner. It doesn't matter how complicated that is. As we'll see, it can get more complicated or better as we work on it.
3:28 And then the game is over depending on how many people have won. Let's try that. It should behave exactly the same
3:34 but it is much easier to think about. And you'll see that it's also easier for us to add the best of five, or best of three, or whatever.
3:41 I'm going to play rock. I threw a rock, paper throws rock. The game was tied. So look, it still works, just like it should have. Let's do one more.
3:49 Scissors, you roll scissors. Computer rolls scissors. Now the computer rolls rock. Game over.
3:55 Ah, we lost. But we won the game of programming, didn't we? Look at this. Look how much nicer this is.
4:00 And if we need to check for winning in other places or we need to get rolls in other places we just do that one line again.
4:07 And it allows us to have a simpler code that we think in high levels about but also to reuse it. 'Cause if you want to call it again
4:14 there's your player_2 against player_1 in a manual way. Right? If you wanted to play computer we could just come down here.
4:20 And I play rock, they play paper, boom, that's it. Look how easy it was for us to convert from an automatic player
4:28 over to one where we have a multi-player sort of situation. And that was easy because this code is incredibly reusable.
4:36 All we got to do is call the function again with different inputs, boom, we're off to the races.

Talk Python's Mastodon Michael Kennedy's Mastodon