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


Talk Python's Mastodon Michael Kennedy's Mastodon