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