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