Python for Absolute Beginners Transcripts
Chapter: Organizing and reusing code with functions
Lecture: Adding a computer opponent

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well this is pretty fun
0:01 and when we run it, we have to ask for the two people
0:04 and then we have to enter the two roles that they play.
0:07 Well, what I would like to do is have that
0:09 always be the computer
0:10 and the computer automatically just play
0:13 not taking into account what I've typed
0:15 but what the options are.
0:17 So that's what we're going to do in this section here
0:20 and it's super easy.
0:21 We're going to need to have the computer
0:23 make some kind of random selections.
0:26 So we already saw the random library, that had randint.
0:29 We're going to use the random library again.
0:31 We'll import random.
0:32 Remember, pretty much enough to type r
0:35 and that's it.
0:36 Right now Pycharm has it gray
0:38 and what that means is you're importing this library
0:40 but you're not actually using it.
0:41 It's unnecessary, your code would work just fine
0:44 if you were to take it away.
0:45 If I hit Enter, it'll just clean those up
0:47 cause it's like, hey this is unused.
0:48 But that's just because we're about to use it.
0:51 So, for player name two, instead of asking
0:54 let's just say this is the computer.
0:56 And then, we could even simplify this
0:58 and make this just you, for now.
1:00 Here we ask: Hey you, what is your role?
1:02 And then, we're going to get something.
1:04 And down here we're going to go
1:06 instead of asking the player for an input
1:08 we're not even going to need to validate it
1:10 cause it's always going to be valid.
1:11 What we're going to do is, we're going to use that random library.
1:14 Now, there's ways in which we could use a random integer
1:17 and the length of the roles and all that
1:18 but Python has a much better option.
1:20 We can just say random, and we don't have to type all of it
1:23 Pycharm will help us there.
1:24 If we have a list like this, of multiple items
1:28 and we want to randomly get one of them
1:29 all we have to do is say choice.
1:31 And, we can come over here and just say choice
1:34 and give it.
1:35 As I say it take a sequence, well let's give it rolls.
1:37 Perfect.
1:39 So, we do this, it'll say: You roll
1:41 Make it like this
1:42 You roll that and the computer rolls this.
1:45 All right, let's give this a shot.
1:46 Will we have it working?
1:47 Notice also I've maximized this just a bit more
1:50 just for more room here.
1:51 Okay, you want to do your roll.
1:53 Maybe we want to ask for your name again.
1:54 We will later.
1:55 I want to play paper.
1:56 You roll paper, computer rolls scissors, game over!
1:59 Computer takes the game, cool.
2:01 Can you see how incredibly easy that was
2:04 to have a computer opponent random?
2:06 Random choice and just set the name, that's it.
2:10 That was it.
2:11 Now we have a computer opponent.
2:12 It's kind of crazy
2:13 Of course, it's not very smart, it's just random
2:14 but you know.
2:15 There it goes.
2:16 The idea though, is that this function
2:18 choice that take data and returns data
2:21 allowed us to work in an incredibly simple way.
2:24 I'm sure the actual details of choice
2:26 is not too complicated
2:28 it can't be as complicated as the random number thing
2:30 but maybe actually use that deep down inside.
2:32 So, here we have a computer opponent
2:35 by just using this function.