Python for Absolute Beginners Transcripts
Chapter: Organizing and reusing code with functions
Lecture: Checking for a win
0:00 Well, that was fun. And now this next part we're going to right here what we have to do is we have to test for a winner, right?
0:06 We've already gotten the rows we've verify that they're valid that they are among the three options that we can do with our game.
0:13 But how do we test who's won? Well, it turns out, the most straightforward way to do this is actually really complicated and not very pretty.
0:22 But what we're going to do is we're going to build this and then we're going to improve on it and improve upon it, okay.
0:26 So let me paste out some little basic steps that say here's how this game is played. So if somebody plays a rock
0:33 and then the other person plays a rock, it's at tie the other person plays a paper, they lose. Other person plays a scissors, they win.
0:41 But let's go over here. And first we're going to need a way to indicate who won this round. So we'll say winner equals, now in Python if we want to say
0:50 we don't know what this value is it doesn't point to anything. Is it player_1? Is it player_2? We don't know, it's not set yet.
0:57 The way you do that, as you say, it's none. This means remember in the Python tutor where we had all those arrows pointing at things
1:03 basically the conceptual idea here is this point nowhere technically there's a thing that's created that's called none, but that's the idea.
1:10 So that it says, usually know here this goes right now. So what we want to do, is we want to convert this bit into some if statements.
1:18 So let's do this first one now. Notice there's a tie, there's a tie, and there's a tie. So scissors scissors tie, paper paper tie, rock rock tie
1:27 Though, let's go over here. And we'll say if, oh, what is it? Row one is the same as or equal to. So double equals, remember, row two
1:36 then we'll print a game was a tie or the play was tied. All right, so now we want to have another case. On the other hand, what are we going to do?
1:46 Well, we're going to start out by just doing basically this test in software. I'm going to show you something much better later.
1:52 But remember that requires more ideas that we're going to get to. So if I can say row one, if this happens to be rock
1:59 now be careful, that and that is not the same. Though if we say capital R rock that's a totally different variable value than lowercase rock.
2:08 So we can actually even verify that a little bit better. So we could take away some of those challenges that the players might have.
2:15 So if they type capital rock or something we could also go over here and say I want to change row one
2:20 let's make it whatever it is, but we want to lowercase it. So that's a string thing. We also might want to say, well
2:26 if they put a space accidentally and then hit Enter that's okay too, We're just going to ignore that. So you can say that by saying strip.
2:32 That we go to this value, and so by it we say row one is its current value, but lowercase and then take away all the spaces, tabs, and so on.
2:40 That'll make things a little easier. So we don't have to worry about checking whether or not that's a capital or lowercase R
2:46 whatever they type is always lowercase rock because what we just did there, right. Now if that's the case we have more tests that we have to do
2:53 so we have to say if row two is, ah, which one, paper. If row two is paper, player_1 is a loser which makes player to the winner.
3:03 So we'll say winner equals player_2. Wow, right. Else, now we could just say else because technically they're not going to be the same.
3:12 But let's say make it really clear. If this is going to be scissors, if it scissors the person whose perspective we're looking at this from
3:22 is going to be the winner. The winner is player_1, okay? We need to just do that over again for paper I'm going to move this up for now.
3:30 So you all have this as a little reference. Like so, and going to be exactly the same. This is going to be the next one is paper. This is rock.
3:42 It's paper and this is rock then the winner is the person playing that row. And still like this. There we go.
3:50 So if I play paper, other person plays scissors, I lose. If I play rock or if they play rock, the paper covers it
3:57 I win, right. Do one more and then we'll have it all, okay. I told you, this is not pretty we're going to make it awesome. Hang in there.
4:03 But right now it's not pretty. The last one is if I were to play scissors and they play rock, they're going to smash the scissors
4:11 so they win. If they play paper, I'm cutting the paper so I win, all right. And let's just do a little print out here.
4:18 So this will be the end of the round, a game is over. Say if, one more test here, if not winner or something like this, winner is none
4:28 then what we can do is we'll print it was a tie, else and whatever the winner is, takes the game, right?
4:36 Though, we've determined the name of the player. So that won, we're just going to say they won. This is what happens if there's a tie.
4:42 Otherwise, somebody has won, let's just say who it is. Wow, okay, that does not look pretty, does it? Well, we're going to play more of that
4:50 and make it much, much nicer. But let's go ahead and play a game and see how we're doing here. Remember, we're going to build it up
4:55 kind of the yucky straightforward way and then we're going to make it way nicer with all the ideas like functions or what not that we're studying.
5:01 My name is Michael, other players Sam. I'm going to play paper. Let's pick some situation where Sam is going to win
5:08 and to win this round he has to play scissors. Michael row is paper, Sam row is scissors. The game is over and Sam takes the game, awesome.
5:16 Let's play one more round. It's getting tiresome to already typed in my name and the other players name, that's fine.
5:22 Let's play paper and he's going to play rock which case I should win. Michael row is paper, Sam row is rock. The game is over, Michael takes the win
5:30 with a decisive throw of rock, no, of paper. Fun, fun fun game. Okay, so it looks like it's working. There's a few shortcomings here.
5:39 One this is you know, Sam can always just see what I type in and go well you know what it's time to play some scissors or something like that.
5:48 The other one is, what if I want to play by myself? I don't have any friends right now that want to play Rock, Paper Scissors
5:54 though I want to play by myself so we can make the computer do scissors. A few basic enhancements there.
5:58 The other one, it's very common that Rock, Paper, Scissors is done in a best of style, right? So, first person to win three rounds
6:07 first person to win five rounds and so on. Well, what we've written so far, is not easy to do but we'd have to like replicate this over and over
6:16 or do some kind of loop or something. So we're going to do a little bit of work here as well.
6:21 More to do but still, Rock, Paper Scissors is coming along.