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