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