Python for Absolute Beginners Transcripts
Chapter: Organizing and reusing code with functions
Lecture: Selecting the roll quickly
0:00 Let's look at what I consider to be a bit of a drawback for our game here. If I run it, I have to type scissors.
0:07 And if I misspelled scissors, like 'ssors then, whoa, whoa, that's not a valid play you got to go again.
0:13 Wouldn't it be nice, wouldn't it be easy, if it could just press one for rock two for paper and three for scissors? That would be cool.
0:20 That's what we're going to do now. Also, going to let us look at a cool different type of loop
0:24 while we do it. Now, let's go back here and kind of go through this higher view of our program that we have because we're using functions.
0:31 So show header, we don't need to mess with that. play_game, that's probably the place we have to go. And then in here, this is the part that
0:39 asked the user to input something and then it gets it back. So all we have to do is change get_roll and if this was used in many different places
0:46 this change would automatically be picked up everywhere. That's cool. So we're going to jump down here to this
0:52 and what we can do is instead of that doing that, there Let's go and show them the rolls. So I'd like to print out something...
1:03 I'd like to print out what various role options there are so there's a really cool way to do that. Anytime you have a list or some sort of collection
1:11 or sequence of things you can go through it using what's called a for in loop. And it's incredibly simple, all you do is say something
1:18 like this for r in rolls. Each time, for each role, there's three... So this sweep will run three times you can just print r
1:28 Let's just print it like this for a second. Let's go and run that and see what happened. Here we go. Look how cool that is.
1:33 So all we got to do is, for thing in collection now work with the thing. Well, that's not a huge difference, is it?
1:39 It just puts them out, top to bottom vertically instead of horizontally. So let's not do exactly that. Let's add a little bit more to it.
1:48 What I like to do is show a number. Now, we could come over here and do this weird thing where I have index equals one.
1:57 We could print a little f-string here. So we could say curly, and we could say index not, and then the roll.
2:04 And here we could have it get bigger by one. Here we go. 123. That works. But it's very much not the right way to do things in Python.
2:14 So instead, let me just give a little head right here. You can get spell checking, by the way, which is cool. We go like this.
2:21 So instead of doing this, what we can do is we can say I would like to get the object and it's index. The way we do that is we say enumerate
2:29 and we pass the thing we would have looped over and we get back here is two things. We get the index and the role. So forget that part right there.
2:38 If we run this, it's going to do exactly the same thing but we don't have to do extra work to do sort of bookkeeping of those numbers.
2:44 Close to what we want, but not quite. 012. All the numbers in Python start at 0 indexes start as 0, and so on.
2:51 So this might be fine but its a little weird for people it's also not very handy for putting your fingers near the numbers.
2:57 So what we can do is we can say start equals 1 and this is a way to pass data to this enumerate function and be more explicit.
3:05 Say, there's a bunch of things that have default values I don't care about them, but this one called start let's set it to 1.
3:12 We try again. Boom. There we go. 123. That's great but the input is still expecting us to type in rock, papers, or whatever.
3:21 Instead, let's change this... elected index or something like that. And in order for this to be, remember, a number
3:31 not a string, that just looks like a number we have to convert this input to there. So we could either wrap it like that
3:38 or we can come over here and make a separate variable so it's more obvious. Actually, what did I call it?
3:46 I called it test. There we go. We could be like this. Now we don't have to ask them for the roles instead we have to ask something else.
3:54 So what we need to do, is we need to know that this number is going to be between sort of one to the number of roles but remember what did I just say?
4:02 That indexes start at zero and they actually go up to 1 - length, it's a little bit weird. It takes some getting used to.
4:10 So we need to adjust that back to convert from a human thinking, from 123 back to computer thinking, 012. It's a weird thing but it happens in almost
4:20 all the programming languages, so we'll just go with it. And then instead of this role thing here, we'll have to say if selected index < 0, <= 0
4:32 or selected index is >= Actually, zero is okay because we adjusted it. But if it's great than the length of the roles...
4:43 That's how many lengths there are in that collection the way we do is like so. Let's change this to selected_index + 1 so that in their mind
4:54 Hey. They still see it as that one they've selected and we could actually just put text. That would be easy. It'll say it's out of bounds.
5:03 And then here we need to return the role from the index. The way we do that is we go to this collection. We say, I know you're a bunch of things
5:11 I want to get one at a certain position that we've selected but we'll just say selected index, like this. Go to the collection.
5:18 Pull out the one that they typed in. This way they don't have to type scissors, rock or whatever we end up expanding this to in the future.
5:26 You just type 123. Let's try that. Which one do you roll? How about two? We say two, you roll, paper. Cool, right? That was rock. Okay.
5:38 Score is we're up one, we haven't won one of these yet so that's cool. Let's play scissors. There we go, we rolled scissors. We'll roll scissors again.
5:46 Oh, computer won. Let's roll scissors again. Let's play one, throw some rocks or else we're in trouble.
5:53 Hi. This is it for all the marbles, unless we tie. Whew, it was a tie. Let's throw rocks again. Man, we haven't won one of these games yet, have we?
6:02 Crazy. But there it is, at this cool way of working with four in loops, allows us to list out all the items. Because we want to give them a number
6:12 we can use enumerate and say go 1234 and give me the item that goes with it. Let's us ask the player for just some kind of number
6:24 in a one, or a five, or whatever the possible options are. I guess we haven't tested our logic. If you enter five right now, it's out of bounds.
6:32 We enter zero, it should be out of bounds. Not for the computer, but remember we adjust that to be one fewer. We put minus 100, still out of bounds
6:41 but one, one works.