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