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