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

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

Talk Python's Mastodon Michael Kennedy's Mastodon