Python for Absolute Beginners Transcripts
Chapter: Cleaner code with common data structures
Lecture: Improving win-tracking with dictionaries

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now let's look at one other thing that we had to do a lot of coordination I guess is the best way to put it in order to keep track of who won the game
0:10 if somebody's won the game and so on. You notice we have to know how many rounds there are. There's how many times has player_1 won
0:17 how many times has player_2 won and then we have to check it like this and we got to check it like that
0:23 and then down here, we have an if else statement where we check if the name is this we can increment that number if the name of that one
0:29 we can increment that number. Woo, that's not pretty. So let's go write another function here. Find a winner. Now find_winner
0:38 is going to look for an overall game winner. So what we're gonnna do is we're going to create a dictionary that keeps track of given someone's name
0:46 how many times have they won and we're going to have a list of names. So let's just say return false for a second. So this whole part up here
0:57 we'll just be able to say we need something let's just type wins that doesn't exist yet and names we're just going to put a list of player_1
1:04 player_2, like that. We could store this in different way but we can just stick it here this can be okay.
1:09 Now wins, let's go and create a dictionary for wins. And it's going to be player_1 is the value. Actually, let's write it like this.
1:19 I'll show you the not easy way but the easy to understand way but not the clean, pretty way and then I'll show you a better way.
1:27 So we can come here and say, like this. Yeah, I guess we can create like this. So we say player_1 equals 0 player_2 equals 0.
1:35 There's some cool tricks that we can do to make that a little bit nicer but that's okay. Let's come over here and get rid of this.
1:41 Now we're going to pass a wins and the names over and this one could even be better. This might be something like wins .keys like this, right?
1:50 All right, those are going to be player_1, player_2. So let's go over and do this find winner. How's this going to work? Well remember wins as the name
1:56 and then how many times each one won. So we'll have how many rounds we want to play and then we're just going to do this for name and names.
2:06 How do we know the game is over? If somebody has won as many times as we're playing the best of. So how many times has whoever this is
2:13 player_1, player_2 won? We'll say if wins, get name that's greater than or equal to best of who won. Otherwise, if you make it all the way through
2:25 however many names there are nobody's gotten more than the best of then there's no winner. Now this might crash potentially.
2:32 So we can put a 0 there just in case. The way we put it together surely they're going to be there but there's going to be, you know a value for that.
2:40 But just to be a little bit safe I'll put a 0 so we get a number back that we can still compare against the best of. Now, that works up here
2:49 but how does this work down there? Well check this out. Here we can just say if there is a winner all we're going to do is go to our wins
2:57 and find whoever the winner is and then increment their wins by one. So all that is now replaced with that one line 53 there.
3:06 We don't have to check if it's player_1 change players one's variable if it's player_2, change player_2's variable.
3:11 No, this data structure, this dictionary holds all of the players and all of their scores so we just have to go find the winner
3:18 get their score and change it. Now, down here and we're going to need a little bit of something as well. This will be player_1
3:26 and this will be wins of player_2. And this part here about the overall winner remember all this, checking here if this or that and so on.
3:36 All of this here can be replaced with a very simple overall winner equals we wrote a function, takes the wins and the wins.keys just like that.
3:48 Again, this complicated if statement is now replaced with just a simple little dictionary thing instead of having multiple variables
3:55 and checking and so on all of that now like this. If we did things right this should work. And the essential thing that we did here
4:02 was right there, was using this dictionary to track the wins. And each time through I'll print out and we'll take this away again
4:12 but I'll print out current win status I'll print out wins. All right should work the same none wins the game. Yeah, I'm going to say no.
4:23 Say well there's not a winner I know a little bit backwards didn't I. Okay, there's not a winner so we got to keep playing.
4:29 Available rolls, what is your roll? I roll rock, computer rolls rock and that means it's a tie, nothing's changed. Okay, I'm going to roll scissors
4:39 computer rolls paper, I win. So here's the current win status. I guess I got to do it one more time need to show it. Here's what it should be
4:47 it's out of order. I got one, computer has 0 but now again I have two so just not printing it soon enough. Now if I were to print it one more time
4:56 and say you three, computer 0. See how we're using this dictionary to keep track of that? I'll go ahead and just move this
5:02 where it should've gone down here and then I'll comment it out. Okay, so we're able to use these dictionaries these data structures
5:10 to dramatically simplify the juggling of well there's three players maybe and all these different rolls and if this player plays that roll
5:18 then we change that variable. No, we just have these couple of data structures that store it and we're only touching the surface
5:25 of how awesome this is going to be. This is going to be way better. Think mods, think all kinds of cool stuff that we can do going forward.
5:33 But for now we're going to leave it here with the same program doing the same thing but much nicer on the inside.

Talk Python's Mastodon Michael Kennedy's Mastodon