Python for Absolute Beginners Transcripts
Chapter: Organizing and reusing code with functions
Lecture: Adding the best-of feature

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, our winner-take-all-thing was fun.
0:02 But like I said, this is not typically
0:03 how rock-paper-scissors goes.
0:05 Usually, it's best of.
0:07 So let's go over here and have rounds
0:10 and let's say we're going to play best of three.
0:12 This would be how many rounds you played.
0:14 And then in order to figure out
0:15 somebody has won the overall game
0:17 we have to know how many rounds they've won.
0:19 So the first person to win three rounds
0:22 is going to win the game in this case.
0:23 So we'll say wins
0:26 p1 is 0 and for p2.
0:29 It's a little clumsy.
0:30 We're going to come up with a better way to do this.
0:32 But, again, we're taking sort of iterative
0:33 little approaches to working on this.
0:35 We're doing simple stuff
0:37 then we're adding more ideas and more programming concepts.
0:39 Data structures will let us simplify
0:41 a lot of these things going on here.
0:43 So what we would need to do in order to play the game
0:45 is, well, we need to go have a loop.
0:47 And we already did one, these while loops.
0:49 And let's do a little test here.
0:51 We'll say, first like this, we'll say while
0:55 wins_p1 < rounds and wins_p2 < rounds.
1:00 'Cause if they're equal
1:01 that means either player_1 or player_2 has won.
1:05 Then we want to do something like this in the loop.
1:08 But we need to record who won that round
1:11 and this also, we kind of want to just print out
1:14 maybe not the game, but the game is over
1:16 this round, this round with a tie takes the round.
1:22 Then down here, we need to do a test.
1:24 So we'll say if wins one is greater than or equal to rounds
1:29 print a little output like So, player_1 wins the game
1:35 else player_2 wins the game.
1:39 Right, so this is a start here.
1:41 Now, the one thing we have to do
1:43 is figure out who has won the game
1:46 then update that.
1:47 So this is close, we're going to have this
1:51 but we have to say something like this.
1:52 If winner is equal to player_1
1:56 then wins_p1 has to get bigger by one.
2:01 If it's two, we want that to be two.
2:03 Now, this could be else, maybe if else makes sense
2:06 but it could just be else with no test
2:09 but maybe we're going to have three players
2:10 in the future or something, I don't know.
2:13 Let's keep it a little bit like that.
2:14 Down here, we could do a little bit better as well.
2:17 We could have something like this.
2:19 Overall winner, both none.
2:22 This would be player_1.
2:24 And we can just print out overall winner.
2:27 So what was less good about it before?
2:30 Well, what if we want to change the text
2:32 of Wins the game or other messages like that
2:35 we'd have to edit it in both locations.
2:37 Writing it like this means that this is only expressed once.
2:42 All we have to do is figure out
2:43 what name goes into the statement
2:45 which is a little more safe
2:46 and less repeating ourselves and so on.
2:49 Okay, so do a final cleanup.
2:52 This is cool. We should be able to have this work once again, I guess.
2:57 Yeah, I think this is going to do it.
2:58 Let's go and give it a try.
2:59 There's probably a little good message we can put out
3:01 like Hey, it's round two, it's round three
3:03 and so and so is in the lead, or something
3:05 but I think this will work.
3:06 Okay, round one, what is your roll?
3:09 I roll rock. You roll rock.
3:11 That takes the round.
3:12 What is your roll?
3:13 On this next one, I roll paper.
3:16 I roll paper, computer rolls paper.
3:17 So now that should be one win computer, zero wins me.
3:22 Let's actually print them out down here.
3:25 We print score is player_1 and player_2 is win_p2.
3:36 Let's do a little separator as well.
3:38 Okay, so now we'll be able to track this a little better.
3:41 So paper, we roll paper, they roll scissors
3:43 oh, I got crushed.
3:44 The score is you zero, computer one.
3:47 It's round two.
3:49 Let's go and say, well, if they rolled scissors
3:52 I'm going to roll rock.
3:53 Of course, it's random, it doesn't matter.
3:55 They win.
3:56 Okay, if they win one more round
3:58 and our program is right, it should exit.
4:00 But let's say scissors
4:02 I do scissors, they roll rock.
4:04 The computer takes the round, three-zero
4:06 I'll get out of the loop because this test up here
4:10 somebody has won.
4:11 I'm going to get out of this loop.
4:12 And then that's it.
4:13 The computer wins the game.
4:14 Let's do one more just to see how this works.
4:17 I'll play a lot of rock, I won.
4:19 I'm up by two, two to one, two to two.
4:22 Here it goes, somebody's going to win.
4:23 Rock, oh no, maybe not, not if there's a tie.
4:27 Oh, the computer won again.
4:28 But pretty cool.
4:29 You can see even when there's a tie
4:31 you know, let's just play again
4:33 it's the best-of-three wins
4:34 not just whatever happens after three rounds.
4:37 So there you have it.
4:38 We now and we're able to use this again over here
4:42 to this. There's one other thing we could do
4:46 that would make this a little bit nicer.
4:48 So this is starting to look a little complicated again.
4:50 All of this stuff here
4:52 it's like well, what is going on here?
4:54 So let's do one more thing.
4:57 Let's maybe make this, could be something like play a round.
5:01 So we come over and we're going to right-click
5:03 and say refactor and see if this is going to come up
5:05 with something reasonable.
5:07 No, no, it can't because this part right here.
5:12 So that's okay.
5:13 Now, one thing that happens is if this is wrong
5:16 before it was just canceling the game.
5:19 If we want to say, you know what, we can't do this
5:22 but let's just go and ask again
5:24 we can change this Return to Continue.
5:27 What that means is instead of going on down this way
5:31 oddly, Continue means go back to the top
5:33 go back up here and just start again
5:35 and then start again until you get past this.
5:37 So let's do it one more time.
5:38 If I play blah, blah, blah, sorry, that's not valid.
5:41 Can't play, exiting, I guess.
5:43 Let's just say Try again.
5:47 Try to play blah, blah, blah, nope.
5:49 This one, nope.
5:50 Rock, there we go, you play rock and scissors.
5:52 So I think our game is in pretty good shape.
5:55 Again, I think we could clean this up a little bit
5:57 but there's not a whole lot to do.
5:58 It's about as good as it's going to get.
6:00 I like it.
6:01 It's so much better and reasonable and clear to understand
6:04 than when we first started, I think
6:06 and we got through it.
6:07 This used to be like complicated.
6:09 It's just that. This was super complicated, now it's just this.
6:12 Pretty cool.
6:14 So hopefully, you can see how functions
6:15 made our code easier to think about.
6:17 We don't have to think about all the details
6:20 of checking if we're winning through
6:21 as we try to understand what this loop does.
6:23 When you have to think about
6:24 all the details of getting a roll
6:26 all we have to do is no, first, we got to get the roll
6:30 also have the computer play the roll
6:31 and then check for a win
6:33 and go and do something based on that.