Python for Absolute Beginners Transcripts
Chapter: Reading and writing files in Python
Lecture: Demo: Saving wins
0:00 Well, the first thing that we need to do
0:02 if we're going to have a leaderboard is figure out
0:04 who is playing right?
0:05 We can't just have you all the time
0:07 and computer the other time.
0:10 So let's do a cool little trick here.
0:11 Let's say, I'm going to write a function
0:13 and it's going to be get_players.
0:15 And what we going to do
0:16 is we're going to use this cool little trick
0:18 where we can return more than one variable
0:20 or value from this function.
0:22 Technically, it's still returning one thing
0:24 but it will look as if we're doing more.
0:26 So we'll say what is player_1
0:28 P1 just to show you these can be distinct
0:30 well if they input player_1, what is your name?
0:34 And then down here we'll say P2.
0:37 For now its just going to be computer
0:39 I guess we could capitalize their name.
0:41 And up here we'll say player_1, player_2
0:45 is equal to get players like this.
0:47 And then as is in like so.
0:51 And now we just need to return these values
0:54 you don't need that anymore.
0:56 We can just say return, p1, p2.
0:58 So we return this as what's called a tuple
1:00 goes back as a pair
1:01 and then it gets set back into those two variables.
1:03 Pretty cool little feature of Python.
1:05 So let's just see that this works here.
1:07 What is your name, player_1?
1:08 My name is Michael.
1:09 Great. I'm going to play rock.
1:11 Michael plays rock, computer rolls paper.
1:14 Again, computer takes a round.
1:17 That's good, and that's important
1:18 because we needed to actually have different names
1:22 so it would put them into the leaderboard.
1:24 We can do that.
1:25 So let's go down here to the play game part.
1:28 And then we go down or we have a winner.
1:30 This right here is great to have the wins for the round
1:35 but actually what we want to know
1:36 is we want to record the winner here.
1:39 Ah, sorry, here, for the overall winner.
1:42 So after this is over
1:43 let's do something like this
1:45 we'll call it record win.
1:47 And we'll pass the overall winner.
1:48 Now, what is this overall winner?
1:50 This is going to be the name
1:52 like Michael or computer or something like that.
1:55 So let's go down to the bottom
1:57 and we're going to define a function, call that
2:00 and we'll change this to winner name.
2:04 Alright, well what we did before
2:06 I'm going to delete this now.
2:07 We got it saved in source control in the history there.
2:10 What we're going to do now
2:11 is something very similar to what we did here.
2:14 So we're going to come up with a directory and a file
2:16 and we're going to kind of do the same thing.
2:21 Change the name of course
2:22 we better not put it in the same file.
2:23 We'll call this leaderboard.json
2:25 and we're going to use JSON here as well.
2:28 We're going to, this is the last thing we're doing a JSON
2:29 we're going to move on to another format momentarily.
2:32 But let's just use JSON.
2:33 You'll see that works pretty well.
2:35 Now we want to write to this file.
2:37 And reading it is not what we want.
2:39 And I'd like the file output stream name
2:41 so I'm going to change that
2:42 technically the variable name doesn't matter.
2:46 And we're not doing it this way
2:47 we're not getting data back
2:48 we're putting data into it.
2:49 So we're saying save, save, let's try save, no.
2:54 Right, nope. Dump. Whatever.
2:58 That's a bad name I think, but that's what it is
3:00 and what we want to save here
3:01 is some kind of leaders or something like that.
3:06 So, where does this leaders come from?
3:08 Well, we're going to need to have another function
3:11 that loads this.
3:13 So I'm going to say, come over here and say load_leaders.
3:17 And this one is going to be really really similar to this one.
3:20 So you'll see.
3:21 So let's go over here and call it load_leaders.
3:26 This one's going to try to read this file.
3:28 Now this is going to be interesting as well.
3:32 Return json.load. Is that going to work?
3:37 Mm, probably not. Let's go find out.
3:40 So UI. My name's Michael.
3:42 I'm just going to play one a lot.
3:44 Oh ho! Error crash, no file, such and such.
3:47 And you can bet that that's the leaderboard.
3:49 If we click here, we can see where it happened.
3:51 It happened right on that line, 116 for trying to load it.
3:56 You can't load a file that doesn't exist.
3:57 So we got to do a little test here
3:59 we got to say first if, if the path exists
4:05 now we want it to be not the case.
4:07 Then, what are we going to do?
4:08 We're going to turn an empty dictionary
4:11 so that we'll get started with no data
4:13 just an empty dictionary
4:14 but if the path does exist
4:16 that means we must have saved a dictionary there
4:17 we're going to load it up.
4:19 Alright. So that should work.
4:21 And then, we're going to do this.
4:23 We're going to say if when a name in leaders
4:28 leaders of winner name plus equals one.
4:31 So we're going to store the name
4:33 and then one, two, three, four, five, six, seven.
4:36 And we say else.
4:39 This is just going to be equal to one.
4:42 Why do we got to do it that way?
4:43 Well, it's a little bit weird.
4:45 The problem is if you try to say +=
4:47 and it's not there, this is going to crash.
4:49 If you did it again
4:50 it would return something funky as well.
4:52 There's other ways to do it
4:53 but I'm going to do this.
4:55 Think it's good enough.
4:56 Now, let's go ahead and play this game.
4:59 Going to be Michael.
5:03 Alright, the computer has a one, no surprise, right?
5:07 But check this out.
5:08 We've got some externally added files.
5:10 I'm going to ignore those for a minute
5:11 but here we have leaderboard.
5:14 With that, the computer won.
5:15 The computer has one, so the computer has won.
5:17 Let's go and play it one more time and see what's happened.
5:20 I'll just leave it here.
5:21 I'm Michael.
5:23 Ugh, the computer won again.
5:25 Notice how it changed to two?
5:26 Man, I better win one of these.
5:28 My name is not going to be two.
5:31 Let's try this again, my name is going to be Michael
5:33 I'm going to play two this time
5:34 that'll trick the computer.
5:35 Ugh, now it changed to three.
5:37 Play it a few times 'til I win.
5:41 Yes, I won the game!
5:42 Now check this out.
5:43 Now I'm up here as a one and so on.
5:45 Whoops, long as I don't mess it up.
5:46 So we're storing this information.
5:48 That's pretty cool, right?
5:49 So now we're recording who the leaders are.
5:51 We're not doing too much interesting with it
5:53 but we do have a persistence across the plays.
5:56 We're trying to load it up.
5:57 If there's no data at all
5:58 we're starting from scratch
6:00 so we create a empty set of history
6:02 or empty set of leaders, otherwise we give it back.
6:04 And then what we do on this one
6:06 is we get the leaders as they are
6:08 and then we update that
6:09 and we save it back over top the same file.
6:12 Looks like it's working great.