Python for Absolute Beginners Transcripts
Chapter: Cleaner code with common data structures
Lecture: Demo: Better rules with dictionaries

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now it's time to work on our rock paper scissor game. We're going to do a couple of things we're going to start out by taking the rules
0:07 that are encoded into the if else statement and encode them into a dictionary. So again, we have the old game
0:13 the game without these data structures there and I made a copy so that we can start you know you can have the old version
0:19 and the new version for your records. So lets open that. And then here, and run this just to make sure it's still working, it says
0:25 oh we don't know where the interpreter is lets go grab one. That'll work, okay. What are we going to roll? We're going to roll rock.
0:31 I guess, lets try that again. We're going to roll one, which is rock and now I roll two which is paper three which is scissors
0:39 and as I keep throwing scissors computers, computers on to us it knows I still am yet to win one of these rounds. But none the less here's our game.
0:47 And let me show you a cool little trick. I'm going to go over here and do a refactor, rename on the project, call this RPS structures.
0:57 There we go, it didn't change the file but then if were back here in our most recently used files you can see that's our old, that's our old one
1:04 and this is our new one. Okay that might help. Also lets give this a proper name here. And do something like this. Put this in like that.
1:14 So we know what we're working with. Okay great how's it looking? Yeah, looks great. So lets look at this again. Down here, remember this beast?
1:22 Make this full screen and give us a little room to work with it, so what we're going to do is we're going to say if there's a tie
1:26 it's one thing, but then we've got to start going okay if it's rock, what defeats rock? Well, paper defeats rock, so the other player wins.
1:35 But what is a losing play against this, against the rock? Well, scissors is a losing play so the person who threw rock won.
1:43 All right so lets just take this bit up here and I'm going to put this to the top. Could put it inside this function
1:48 but that means every time you call it we're going to compute this data structure. Not terribly expensive but, you know
1:53 it really doesn't change so lets put it up here like this I'll call it rolls like that and lets just put, inside you can put what's called
2:02 a literal string here like this just so we have it for a minute. So it doesn't mess up our formatting and what not.
2:07 But we're going to have a couple of keys, have that. That's going to have the value of none. That's fine. You got to put a comma.
2:15 We're going to have one for paper. And we're going to have one for scissors. So the idea is we're going to go to this rolls thing
2:23 and we're going to ask, Hey I have paper somebody rolled paper, I need to compare it to another play. So we could have none, we could have seven
2:30 we could have whatever but I also showed you that we can have other dictionaries that are in here. And this dictionary is going to contain two things
2:37 defeats, with none for the moment, and defeated by. So what we're going to do is we're going to write down what defeats rock, well what defeats rock
2:48 paper defeats rock. At the moment we have one as you'll see we could have many, many things there. And actually lets put this as a set
2:56 lets put it as a list, honestly sets probably better but where we're going you're going to see that lists going to work slightly better.
3:02 Cause like I said there could also be sponge there could be other things that go in here but for now there's just one.
3:08 What is defeated by rock, well scissors are defeat by rock. Okay so that's the thing that we put for rock
3:15 we're going to go get rock if that's what they've thrown and then we could be able to ask, This other throw
3:19 is it in the defeats, this other throw is it defeated by? And that'll help us answer the question. And these are going to be exactly the same shape
3:26 but not the same values so paper defeats rock and paper is defeated by scissors. This one is not right. I have this backwards hold on. Paper, scissors.
3:38 Rock beats scissors, rock is defeated by paper, okay. Paper defeats rock, but paper is defeated by scissors, okay. One more time.
3:45 Got to do the same thing down here. What defeats, what does scissors defeat. Paper. And it's defeated by rock.
3:53 Okay so we have this data structure here now might be wondering well, okay cool, that's nice Michael but what does that actually get us?
4:00 It gets us two things it gets us a much simpler way to check whether or not we've won. I'll show you that in a second
4:05 but it also gets us the ability to add incredibly easy like if we want sponge we just do one more line here, sponge. We won't even have to rewrite
4:14 the code that checked for the winner. Just, this data structure will store that.


Talk Python's Mastodon Michael Kennedy's Mastodon