Python for Absolute Beginners Transcripts
Chapter: Organizing and reusing code with functions
Lecture: Demo: Starter Rock Paper Scissors

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, with our rock paper scissors we're going to start from the beginning. So let's open up PyCharm Community Edition here.
0:08 And here's our old M&M game, but we want to create a new one. Now, we're going to create our project over in Ch06
0:14 Organizing Code with Functions, in our GitHub repo and we're going to call the overall folder rock's game.
0:19 It's not actually really going to come into play so you can call it whatever you want. We're going to let PyCharm create
0:24 a new virtual environment based on Python 3.7. Virtual environment is not really required but it's good practice to have it for later.
0:33 Right, here we are and we have no files yet there in our virtual environment, which is ignored. So let's come over here and create a new Python file
0:41 call it, hold on, rpsgame, our game like that. Now, here's our game. Remember to start running it. We're going to right-click here once and then Run
0:51 and then now we can click here. Use Control + R, there's all sorts of ways. It does nothing but exit with code zero. That's a good sign.
0:57 That means that Run it just doesn't actually do anything yet. So what we need to do is we're going to write this in a simple form
1:05 like we did with our M&M experiment and then we're going to organize it and make code reusable and simple more simple, and whatnot, using functions.
1:15 So let's just put a little header here something like this. And then in between, we'll put rock paper scissors
1:26 v1, be our first one, put that right at the top. Now, in order for us to play the game we need to know what the rolls are.
1:34 We also need to know what the players are. So let's say player, player_1 in the input, enter player_1's name, like so
1:44 and then we'll do that for player_2. That's going to get us our two players. And then we also need to know what rolls are available.
1:53 Now, we could have, these are separate variables but it turns out it's much easier to work with it if we have some way to say this data structure
2:01 or this variable, it holds all of the potential ways in which you can play the game. You play a rock, could you play scissors?
2:08 Yes, but no, you can't play sponge or whatever. So we're going to have this thing called rolls. This is going to be one of these lists
2:15 that we've used before we haven't talked a ton about. So we put square brackets. This is going to create a list multiple things we can put in there
2:23 and then we just put multiple strings separated by commas. The first one is going to be rock, paper and you know what the last one is? Yes, scissors.
2:32 Well, let's just see how we're doing. One of the things I like to do when I'm working on programs especially when something is new to me
2:38 is take baby steps. We don't want to try to write the whole thing or worry about the whole thing. Let's make sure that this part works.
2:44 So let's print out the rolls that we have and let's print player_1 and let's print player_2. Let's just run this and see how we're doing so far.
2:53 player_1's name is Michael. This is Sam. Okay, so we got, our rolls are rock, paper, and scissors. Michael and Sam are the two players.
3:03 Okay, it looks like that getting information is off to a good start. Now, let's figure out how to play the game, to do the rolls.
3:11 So the next thing that we need to do is we need to actually ask each player what they want to play. Now, in real rock paper scissors
3:19 you go one, two, three, shoot and then you both reveal your play at the same time. It's not easy for us to do that here yet.
3:26 So what we're going to do is we're going to ask player_1 and ask player_2, you know, you're going to assume that they're really not seeing that.
3:31 We're going to make this more automated as we go. Let's start out simple. We'll call roll one is equal to something like this.
3:38 We put an input and we'll ask for the player so we want to refer to them by name make it nice and personal, so let's put a f-string here
3:44 and we'll say player_1, what is your roll? Rock, paper, scissors, something like that. So they know that those are their choices.
3:52 And then let's do roll two, is player_2. And that's what they're going to roll. So let's print out, just a quick thing
3:59 player with the name player_1 rolls roll one and player_2 rolls roll two. Again, just going simply along here to make sure everything is working.
4:08 So Michael, Sam is what I said. I'm going to play rock and Sam is going to play scissors and get crushed, by the way.
4:17 Cool, Michael rolls rock, Sam rolls scissors. Of course, I won. The game doesn't know that I won that round because it's not sure how to do that.
4:25 But so far, we're doing pretty good. I feel like we've got this input thing working alright. What else do we want in here?
4:32 Well, we're going to need to come down here and say test for a winner. And let's start out with a real simple version where we just play one round
4:42 and it's all or nothing, all right? They play their two rolls, each one rolls what they can and then it's over. Either win, you tie, or you lose
4:51 it's just all or nothing. We could also do a little bit of validation if we really want it here. So, for example, we have those like this.
4:59 We could say something like come down here and we could check that what they've typed is a valid roll and this is super easy to do.
5:07 We could say if roll one not in rolls remember, we're doing a test. With if, we can test or whether or not they're just in this list like this.
5:17 Here, we're saying it's not in there. And if it's not, then we're going to print: Sorry, player_1, roll one is not a valid play.
5:26 Let's put that right here like this for now. And, again, for player_2. Notice that we're reusing this is exactly the same code here as it's here
5:38 but we're passing in different data. So it turns out that this is a place that we could really easily check to make sure we're doing the thing right
5:44 and reuse that code. But let's just do this. And then here we'll say I guess we're not doing anything yet
5:50 but we can just check at least that we're doing it right. So Michael, Sam, we'll call those two. Rock, that looks good. I'm going to go for sponge.
6:00 Sorry, Sam, sponge is not a valid play. Cool, huh? So we're double-checking to make sure that only valid values are here.
6:08 There's not a simple way to say stop without just killing the program at this point. We'll see when we're doing loops or other things
6:14 so we can expand on that. But here we're off to a good start.

Talk Python's Mastodon Michael Kennedy's Mastodon