 This is going to be the final video where we get our game in Python speaking to us via a text-to-speech engine. Similarly to how we did that in Scratch where we use the text-to-speech engine and we've got some available blocks. This is going to be the trickiest tutorial because I can only hold your hand so far. It's going to be up to you to install some things on your machine. I can't possibly be there for you, but I know that you've got this. So let's get stuck into it and get our game speaking to us. Music Hey crew, it's The Surfing Scratcher here, teacher-surfer programmer and on this channel I help curious learners just like you along on your learning journeys. Welcome back to our From Scratch to Python series where we created a game in Scratch and we're translating across to Python so we can get some familiarity with a text-based programming language. The first thing that you'll need to do is jump across to Google and Google this PyTT-SX3. I'll leave a link down below in the description, but we want to click on the first result and that will take us to this page here which is the text-to-speech engine that we're going to be using. We're going to come back to this page to install it but for now we're just going to scroll down and you can have a read of the project description there and we're more interested in this usage. So what we're going to do is implement the code first and then we're going to install it. The reason for that is we can't use it on Replet. We have to install it locally on our machines and I'll walk you through that process midway through the video. But for now, in this block here where it says usage, let's copy all of these four lines. I've just highlighted it, command C or control C on A windows. Let's go back to Replet. Okay so back here in Replet we're at the top of the file in our main.py file. I'm just going to paste those three lines there. You'll see the first one of those lines is this import pyttsx3. So we're importing that library into our file here and that's going to give us access to all the functions that we can use. Okay now we've got these next three lines here. I'm actually just going to cut those lines because we don't want to use them here but we do want to use them. So let's cut those lines and I want you to find the function in here that is our say word block because that'll be the first function where we will implement our text-to-speech. Okay so I've gone down here and I found our say word function and there is our to do comment to implement the text-to-speech. So I'm just going to give ourselves a little bit of space and paste those three lines. I'll fix up the indentation there just by pressing the tab. So let's just go through these lines line by line. So we've got engine. Engine is just a local variable inside this function and we're assigning pyttsx3.init and all this is doing is just initializing the text-to-speech engine. Okay and we're storing a reference to it in this engine variable. Then what we can do is call this say function on the engine and we will save some text here. Now I'm going to replace this text because we don't want to say that text. We want to say the word that we pass into here. All right we're assuming that this word is equal to a string. The last thing that we do is on the engine we call run and wait and this function starts the voice speaking to us on the screen. It will say what that word is, it will run it and we will wait until it's finished. What I'd like you to do now is copy those three lines and in the say sentence function just beneath it we're going to paste those lines there as well. The only thing we're going to change is instead of word we're going to say the sentence. The last place we'll need to implement our text-to-speech engine is in the ask the user to spell a block because we're going to clear the console so the user can no longer see the word on the screen but then we need to speak the word and that's why in previous tutorials we inserted this word parameter and now we can just paste that in there because we want to say the word. Now just before we move along I'm just going to jump around and I'm going to reduce our delay here from four to one second because when I run this before the run and wait block it can take a little bit of time so we won't necessarily need that delay there anymore. Okay the last thing that we need to do in our project here is scroll all the way to the bottom and just under our start spelling function definition we need to also start spelling so I put this in here and it needs to be on the level of indentation on the first level so no indentation here you can see that we've got a line running down from the depth of the function and it needs to be on that level if it's one level in it's going to be inside start spelling and this would be a recursive function so we need to be out here and the reason we do this is because when we run the Python file we execute it in the command line or the command prompt we need to call this function to start our game so this is pretty much the same thing as when the green flag is clicked in scratch I'm just going to scroll back up here back to our ask user to spell word and you'd notice that we've got this line of code and we're kind of repeating it here so something that you could do is wherever you see code duplication like this it's a good call for a function or a good timer opportunity to make a function to wrap all this code but we'll press on with the tutorial and it's as simple as that that is how we're going to implement our text to speech engine the problem is we can't run it in replet we don't have the browser capability to be able to use text to speech with this library I tried it a couple of times I couldn't get it working so we're going to have to do it on our machines so let's take you through the installation process of Python and pip and the text to speech engine okay what you will need to do now is depending on whether you're on Linux Windows or a Mac machine you'll need to open up your command prompt on a Mac that is terminal and I'm just going to open up what's called item here so there we go I've got my command prompt open and I'm inside my directory now to see if Python's installed you just need to type Python 3 dash dash version and you'll see if you've got a Python version here that will be all good this will be referencing the main installation or the system installation of Python but if you don't see Python here then you'll need to install it let's go back over to the browser to show you how to do that okay so we're just over here in Google I've just typed in installpython you can download the latest version of Python from the python.org website and it will take you through a package installation of Python for any of those so that's a really quick way to go and install Python on your machine if you scroll down I like the guide that is by realpython and this guide just goes through the steps of installation on all the different machines and a couple of different ways to go about it what you do here is just choose the machine that you're working on and go through the installation setup now back over here in Google there's a link to how to install Python the smart way and what this is referring to is creating a virtual environment for Python what this means is that it will create like a copy of the Python like a clone of it if we're talking about scratch language and place that clone inside of a folder so we're not messing around with the main version of Python on our system and we might do that is so that we don't run into conflicts down the track when the Python version gets upgraded so if you're interested in that I check out this how to install Python the smart way and it will take you to this article okay so now when you type in Python 3-version you should get a version of Python and there's something else that we need to check that you have installed and the way that you're going to do it is type Python 3-m and we're going to be referring to pip here which is a package install of a Python and press dash dash version so make sure that you have got it installed here if you don't have pip installed you could use homebrew to install it or you can check out this link here and it's down in the description you can bootstrap it by just copying and pasting this code on the device that you are working on okay I'm assuming that you've got pip now installed either using homebrew or through using this command prompt now let's go back to our Python text-to-speech engine page okay I'm back here on the Python text-to-speech engine page and what you'll need to do is copy this link up the top now pip install py ttsx3 let's just copy that to the clipboard and head back over to our command prompt now I'm going to paste that into our command prompt and what this will do it's going to tell pip to install this library hit enter and you'll get a whole bunch of output here so I've already got this library installed you'll see that the requirement already is satisfied but it might take a little bit longer for you to do this now you should have that Python text-to-speech engine installed now let's head back to replet okay we're back over here in replet and the stage is set we've got the text-to-speech engine installed we've installed Python now all we need to do is run it on our machine the way that we're going to do that is head over to the files button here and press the three little dots in line on the same bar as the files not just on the main.py okay we're going to download this whole package as a zip now that'll be in your downloads folder or another location that you specified we can head back over to the command prompt cool so I'm over here in my downloads folder and I can press a command called cd which stands for change directory now over here on a Mac I can locate where that spelling game is and I can drag and drop to get the file path of where it is you can just type this in manually as well but I'm not going to do that so I'm going to delete main.py and we're going to just go to the file extension for where that spelling game is and then we'll press enter cool so now we're in the spelling game folder cool and the last thing that we need to do is run the Python program so we can just type Python3 main.py and as I hit enter with any luck we will be able to start to play our game with the text-to-speech engine all right let's do it dash should she try to make a dash for the car dash okay then we type in the word press enter bath and we get our next word take a nice warm bath and relax for a while bath okay so as you can see there there are some delays between going from the text that's on the screen and the text-to-speech engine so you might want to go back into that Python project and modify some of those sleep values to your preference that's it you have just completely translated a scratch project into a Python3 project congratulations that ends the coding component to this series in the next video we're going to check out some refactoring we're just going to go and revise a few things that we've done and I want to show you where you can go to next what are some resources out there if you are pretty interested in Python and you want to continue your Python journey so I look forward to catching you in that video but until next time I'm off to go find a wave I'll catch you in the next one