Python for Absolute Beginners Transcripts
Chapter: Using external Python packages
Lecture: Word completion, basic edition

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We have a nice, colorful game.
0:01 It communicates a lot back to our players
0:04 but they still have to pick one, two
0:06 three, four, five, six, and it will be better
0:08 more natural if you said, I want to play air
0:09 type a, enter, and you got that
0:13 or I want to play rock, type r, enter and go
0:15 and have this auto-complete thing
0:17 that we saw in our example before.
0:19 So up at the top, we're going to start using
0:21 r, prompt_toolkit.
0:23 Now we actually want to get a couple things
0:25 We'll just say from prompt_toolkit import prompt.
0:28 You're going to read the docs about how prompt_toolkit works
0:30 to see how this is, and we also need another thing
0:33 to get the completion to work, so prompt_tollkit.completion
0:39 word completer, is what we're going to use.
0:41 So this will do the prompt and this will actually
0:43 show the little completion thing.
0:47 Now, as we've broken our program into these functions
0:50 it turns out, it's really nice, you go over
0:52 to this play game, remember, we already have this function
0:55 get role for our players.
0:57 If we just go here, and look at that
0:59 this function already knows
1:01 how to talk to the user, validate it
1:03 and those kinds of things.
1:05 So what we can do, is we can actually
1:07 Oh maybe I'll leave a copy
1:08 of this here for you, open it out.
1:10 We can go and change this
1:12 to actually show something different.
1:14 We're going to just change it
1:15 to use the prompt_toolkit
1:16 and that'll be great, it'll just upgrade it
1:19 and no other part of our program has to change.
1:22 So let's change how we're showing this here.
1:25 We can do a little printout there
1:27 and we can printout the role names
1:30 just directly, like watch this
1:32 Just change that little bit for now.
1:34 If we run this, 'My name is Michael,'
1:40 I get the typing in the right place
1:41 Come here thing!
1:42 If I type it right there
1:43 you can see it prints this out
1:45 with the brackets and the quotes.
1:47 It's okay, but it turns out there's
1:48 a really simple thing we can do
1:50 to make this better.
1:51 We can come over here, and strings like just this
1:56 this comma right here, has a cool function
1:59 and we can say join
2:00 and given any other set of stings
2:02 it'll put them all together
2:03 and just separate them by whatever's here.
2:05 If you had a dash, it would be dashes.
2:07 If you put a comma it's that.
2:08 You put a space, it's comma space
2:09 and now if we run it
2:11 you're going to see we have this nice, little description.
2:14 So that'll let them know what to type there
2:18 and we're not going to ask them, this way anymore
2:20 we're going to ask them with the prompt.
2:21 So we're going to say prompt
2:23 Oh look at that, completion information up there.
2:26 So we're going to come here
2:27 and we're going to give it the name, player name.
2:30 What is your role?
2:32 Something like that, you know
2:33 we also need to give it one of these completers.
2:36 So we can come here and say, completer equals
2:40 and let's come up with something like that
2:43 and we've got to create one of these things
2:44 and this is the word completer that we came up with
2:47 and it's really cool. All you have to do is give it
2:49 a list of words it can work with.
2:51 Well, what are the words we want?
2:53 Role names, that's it!
2:55 How cool is that?
2:56 But this is going to come back with the role.
2:58 It's going to be this, and now our test
3:01 is going to be different.
3:02 When I say if not role, they didn't give us
3:04 anything, or role not in
3:08 role names.
3:10 We're going to go like this
3:11 It's not valid or something like that.
3:13 Not valid.
3:19 And here we can just return, role.
3:21 Well, I think that does it!
3:22 We were able to change from this built-in input
3:25 to this completion one.
3:26 Let's see how it works.
3:28 Now, it turns out, this does not work
3:30 inside of PyCharm.
3:31 This little run thing that's down here
3:33 this is not a real terminal or command prompt
3:35 and so the interaction's just not going to work.
3:38 So that way, you have to run it over here like this.
3:41 There's a way I figured out how to run it
3:43 is I clicked run here, and then I went up
3:45 and I just copied this whole bit in the top
3:47 and pasted it into the terminal and command prompt.
3:50 So we do that, right here.
3:52 My name is Michael, fingers crossed
3:54 I want to play scissors.
3:56 Oh my gosh, look how awesome that is!
3:58 Scissors, we can do sponge.
4:01 I can do stuff like air, paper.
4:04 Now, what I'd like to be able to do
4:05 is have stuff like, if I type R
4:08 also have that pull up water and air
4:10 because it has it in there.
4:11 You can debate whether or not
4:12 you just want to have the stuff where it starts
4:15 but here we can pick sponge
4:16 sponge works if I put junk.
4:18 Sorry, that's not valid, try again.
4:20 How about sponge again, my favorite.
4:24 Of course the computer beat me
4:25 but that's fine.
4:26 This is cool right?
4:27 This is working really well
4:28 and yeah, it gives us basically auto-completion
4:32 on all of the words that we pass to it...
4:35 down here.
4:38 Anything in that list, it has to start
4:40 from the beginning, so it's kind of
4:42 you got to type it in it'll complete it
4:43 it won't give you sub-names.
4:45 The word completer will just complete
4:47 whatever you type of any array of text
4:49 or list of text words that you put in here
4:52 and that's how we add it.
4:53 We just call prompt, give it the message
4:55 and give it this completer
4:57 to have it show it as we go.
4:58 One final thing though
5:00 if we'd run it here, and I try to run it like this
5:04 notice warning warning, output is not a terminal
5:06 my role is, I guess rock, it sort of still works
5:09 but you can see this is not good.
5:11 So let me just throw in a little check here.
5:15 Remember, I told you this doesn't work in PyCharm.
5:17 So what we can do is say
5:20 when PyCharm runs our program
5:21 it's sets an environment variable
5:23 called PyCharm hosted to one
5:25 and we can check, are you running in PyCharm
5:27 and if that's the case
5:29 what we're going to do is
5:30 we're going to give a little warning
5:32 and then just ask.
5:33 So now if I run it, it kind of falls back
5:36 to the old way.
5:38 Warning, fancy prompt doesn't work
5:39 run it outside to see it in action.
5:41 What's you role, it's rock.
5:45 Oh, yeah, the reason that crashed
5:48 is that I typed capital Rock
5:49 course I've got to type lower case rock.
5:51 There we go...perfect..
5:52 The validation is not in that part.
5:54 We really don't mean to play this version that way
5:56 we are meant to play it this way.
5:59 Notice, no warning.
6:02 My play is...
6:04 sponge.
6:05 There we go...
6:06 Perfect! So, everything's working great
6:08 with our basic version here.
6:10 Turns out we can do a little bit better
6:11 but we've already gotten quite a benefit from having this
6:16 and let's clean that up.
6:18 Here we go. Love it!