Python for Absolute Beginners Transcripts
Chapter: Cleaner code with common data structures
Lecture: Demo: Checking for winner with new rules
0:00 So this down here
0:01 is going to be a little bit different.
0:03 What we need to do is we want to get just the keys
0:06 not the values.
0:07 So we can come over here
0:08 and say this is going to be a list of rolls
0:11 .keys with roll names.
0:14 Come here and say keys.
0:15 That's just the first levels.
0:17 So it's, you know, rock, paper, scissors
0:19 not their values.
0:20 And then we want to turn that into a list.
0:22 So we're going to go down here and say
0:23 get the rolls for that person
0:24 get the rolls for that person.
0:26 Great, now here's that check for winner thing
0:29 gets interesting down here.
0:31 Now remember all of this.
0:32 Woop, tough battle winner
0:34 well if they tie ya know what they still tie.
0:37 It's totally true.
0:38 So what else can we do?
0:39 We come here and say
0:40 let's say, outcome equals what are the rolls
0:43 I'll get the value from roll one.
0:46 So here's the thing player_1 plays roll one
0:49 and so far we've been playing everything
0:51 from the perspective of that player.
0:53 So what we are going to do is we're going to come down here
0:55 and we're going to try to decide
0:57 does this roll that the player_2 played
0:59 does it defeat roll one or not.
1:02 Let's just do a little print out here.
1:04 I'll do a little f-string
1:05 so we'll say roll one gives us some value down here.
1:09 This outcome.
1:10 Just so you can see what's happening.
1:11 Now we are going to run this and I'll say
1:12 If I play rock, what are we going to get?
1:15 This is the value that comes out.
1:17 So we said rock and these are the outcomes for rock.
1:19 If the other person plays scissors
1:21 or anything that's in this list on this part
1:24 it defeats those things.
1:25 But if anything that the other person played is in this list
1:28 it's defeated by.
1:29 What we can do is we can say really simply
1:32 if roll two in outcome .get
1:36 lets be sure we got the right value on defeats.
1:40 Okay, if it's in this set that it over here
1:44 then what happens? That means we found out what roll it was
1:47 the thing that the other person played
1:50 is defeated by roll one.
1:51 So we will return player_1
1:53 it'll say elif roll two in outcome .get.
1:59 Well we want this other part of the value over here
2:01 with their defeated by fits in this thing.
2:05 Return player_2 and let's get rid of this.
2:07 It's possible that what we'll get back here
2:10 is not a value.
2:11 And if we remember
2:12 if you ask for a value that doesn't exist
2:14 like they played something that's not in our rolls
2:16 actually for some reason.
2:18 This is going to return none when we try to access it.
2:20 This way is going to give us an attribute error.
2:23 So what we can do is we can simply say you know what
2:25 just give us an empty set, an empty list here like that.
2:30 If it's something they're asking for that doesn't exist
2:33 just give back a no data actually
2:35 lets say we're going to need a dictionary there we go.
2:38 So we're going to have this.
2:39 Now lets go ahead and try this out
2:42 make this roll names and use that down here.
2:47 Alright, We've made some significant changes.
2:50 Lets see if things are still hanging together
2:52 and rerun the program.
2:54 Right, we'll play rock and see how this goes.
2:56 You play rock, the computer plays scissors
2:58 what happens? You takes the round.
3:00 Now you win! Yes! That's right.
3:03 And how did it know?
3:04 Because when we played rock
3:06 it said okay I'm going to go get the rock data.
3:08 The key is rock.
3:09 Here's the data and then the computer played scissors
3:12 so it said, is scissors in defeated by
3:15 like is the rock defeated by.
3:18 Well paper is in there but not scissors.
3:19 We ask is
3:20 the thing that the computer played is it in defeats?
3:23 Meaning the rock defeats it.
3:24 Yes, so we won and that's right.
3:26 Now let's play the next round.
3:27 Going to play paper this time.
3:29 You roll paper, computer rolls paper
3:31 that didn't test anything. Do it again.
3:33 Man, they love the paper. Okay.
3:35 Here we go.
3:36 You rolled paper, computer rolled scissors, so...
3:40 I rolled paper and then we checked.
3:41 Do we defeat it? No, it's not in that list.
3:43 Is it defeating us?
3:45 Yes, so there we go.
3:46 Let's try, we'll play some scissors
3:49 computer rolls scissors it's a tie.
3:51 You roll scissors, they roll paper.
3:53 Again, we defeat paper.
3:55 Imma just keep going with scissors.
3:56 Yes! I finally in this course of one this game amazing
4:01 I throw scissors, computer rolls paper we take the round
4:04 and the game.
4:05 Cool huh?
4:06 So this is a really easy way to store
4:10 the relationships between the three rolls that we can play.
4:14 What is defeating each roll
4:15 and what each one of those rolls defeats.
4:18 This might seem like well not that big of a difference
4:21 I mean yes this definitely better
4:23 but above we added some complexity right.
4:25 But what you'll see
4:26 is we can actually do even more
4:28 interesting and powerful stuff with this data structure.
4:31 Now that we have it like this
4:32 instead of just in a bunch of ifL statements.
4:35 Also here is kind of a cool feature on PyCharm.
4:37 And come over here and say local history, show history
4:42 and it can show us some of those changes there as well.
4:45 But here you can see down here.
4:47 What we have now is this and all of this business right here
4:51 is whatever I replaced.
4:52 With just this little tiny bit right there.
4:54 So that's pretty awesome
4:56 and its much easier to just verify once
4:59 that this thing works.
5:00 The other thing that's cool is it's not really explosive
5:03 in commentarial ways right if we want to add two more things
5:07 yeah we should add
5:09 maybe like this and we do have to put them in here.
5:12 But it's just not as big of a complexity explosion
5:16 as if it's all in ifL statements.
5:17 Alright, so this is how we're using dictionaries
5:20 to improve the wind checking in our game.