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

Talk Python's Mastodon Michael Kennedy's Mastodon