Python for Absolute Beginners Transcripts
Chapter: Reading and writing files in Python
Lecture: Demo: Displaying leaders

Login or purchase this course to watch this video and the rest of the course contents.
0:00 It's great to have this leaderboard but it's not so much fun if we can't actually see it, right?
0:07 So what we're going to do is, we want to come up here to the top and we want to call something up here, after sure headers
0:12 where we can show the leaderboard. So let's go over here and say, show_leaderboard. like this, and when we just maybe put it right down here like this.
0:21 Daft show leaders, get everything lined up just right. Here we go. Now, what we need to do is load up this file. We could go write that code again
0:31 but we actually already have everything working. We just have this load leaders function already. We could print out leaders and see what we get.
0:38 Let's run that. There's the leaders, that's not incredible but it is showing it right? So, let's go ahead and put this out here, different way.
0:49 Instead of doing that, let's first of all just try to print out a new line, or print leaders. Before, somehow I want to get the values in here.
0:59 Now, when you have a dictionary, there's some cool ways to print this, not to get to go through them and get information out.
1:06 So we could say: name, wins in leaders.items. If you ask for items, you get both the key and the value back.
1:16 So, we just print name and wins for a second just to see what comes out. Look at that, that's pretty cool!
1:21 Not quite formatted all the way, but pretty good. Do a print here, like that. And one more print. Okay, so instead of doing this
1:30 let's put a little f-string in here. We can put the number of wins, like this. And if you had high scores, like 14,220
1:39 you want to comma-separate those, you can do this little trick and it'll do digit groupings like: 1,000. And then, well maybe put the name.
1:50 That looks good. Let's put some dashes, like that just to give it a little formatting. So you can see, we have the leaders
1:55 three for computer and Michael has well, one, I guess that's better than nothing. Let's try to play one more time.
2:03 I'm going to play scissors, I'ma play some of this play some of that. Oh, here we go. Yes, I've won the game. Let's run it again.
2:11 Remember up here, before I run it, had one win. Run it again. Look at that. I'm moving up the ranks.
2:16 Wanted to try to play while here, I'll play Hannah. Do some Hannah here. Player one. Four. Oh, it's not looking good for you, Hannah.
2:25 The tie. Oh, the computers won, got to play another one to get her name to show up in the leaderboard. No, not again.
2:38 Yes, finally! Hannah has won the game. So now she should show up here. Look at that, it goes six, two, one. Now, this looks great, right?
2:46 That's how leaderboards work. It has the highest one and then the next highest one and then so on. But it's just a coincidence.
2:53 If we go over here and we like change the order with the computer last, now it goes two, two, one, six. That's not right.
3:03 So we got to do a little bit more work to fix this. Now, dictionaries, they don't have a sort, a sorted order.
3:10 So what we need to do, is we need to go and say sorted names or something like that. What we come up here and we can start out
3:20 by having this just be the value. So we could say this is going to be a list of leaders.keys, like this. Now this is going to give us the keys.
3:31 Actually I think we could do this better with items. Yeah, do with items. Number of sorted name, not sort. Now, this is not at all obvious
3:38 but it is incredibly cool. See that key thing right there? I want to say key equals, and in Python you can create
3:45 these little tiny expressions that are like functions called lambda expressions. So let's say lambda, and then you say the variable
3:52 that comes in, I'ma say L for leader right, the leader entry. And what we're going to be able to do is you just say
3:59 what value do you want to sort, this went by. And it's going to go through and get the values for all of them and it'll do a natural sort and out.
4:06 Like, for example, we could say here's a date like if it's a user, when were they created. We want to show all the, sort by the newest to the oldest
4:13 or oldest to the newest and so on. So here we can get the things that come back. The name is the key, it's the first item zero.
4:21 The second thing that comes back in items is the wins, it's going to say that. Now if we runt his, I'ma go through here. So this where the leader
4:31 this here, like so. Let's rename this to sorted leaders or something. Let's go and run this again. Now it's sorted. Awesome.
4:41 But it's sorted in the wrong way, not awesome. So, one more little technique here. When we go to our sort, we can come down here
4:48 and say reversed is true. So instead of sorting it smallest to highest highest to lowest. Phew, look at that. There we go.
4:57 Now no matter what order they're in it's going to show the leading leader and then the next one and the next one.
5:02 Maybe we only want to have five in our leaderboard. So there's one more cool trick you can do in Python. You saw that I can get into list elements
5:10 like two, or five, that would be the sixth element. You can get ranges of these. You can say zero to five, like this
5:18 I mean zero to two, so you just see the top two. Like Hannah won't show up. Here you see just computer and Michael. So if we put just zero to five
5:27 then our leaderboard won't show everyone who's ever played but just the top five players, because it's sorted. That's pretty cool, right?
5:35 So, here's our leaders, maybe I'll put one more little divider here like this let's run and see how it looks. I'm not sure I like that divider
5:41 but there it is, we've got our leaderboard right there. Let's play one more game, let's see if Hannah can take another round here.
5:52 She does. She's up, now it's tied. Right now it's tied, just see who's going to go there's no rule for figuring out who goes first
5:59 when they're tied, so-- Guess we'll just get lucky, whoever is in there first. All right, well that's it, that our leaderboard.
6:05 We did some interesting stuff. We were able to record down here. Win by change of this to write and then using JSON dump
6:15 instead of json.load, there's no problem here Python just thinks it's misspelled. That's just what I want to call it.
6:20 So here we go. And then we're going to show our leaderboard over here, nice and easy. Just load it up, use the same function other parts
6:28 of the program are. We have to do this cool little sorting trick here. And then we're going to just print them out. Pretty standard.
6:36 Wow, that leaderboard is awesome, I love it.

Talk Python's Mastodon Michael Kennedy's Mastodon