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


Talk Python's Mastodon Michael Kennedy's Mastodon