Python for Absolute Beginners Transcripts
Chapter: Organizing and reusing code with functions
Lecture: Selecting the roll quickly

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