 We've implemented all about say words, say sentence, ask user to spell and check resolve blocks. Now we need to create this whole block sequence. So let's dive into Python and get stuck into it. Hello world, this is Serving Scratcher here, teacher-server programmer. And on this channel, I help curious learners just like you along on your learning journey. In this series, we've built a project in Scratch and we are translating it across the Python. So we can take you from Scratch to Python. We're nearing the final stages of our game. We're up to the part where we're creating the spelling component in Python. Okay, the first thing that we need to do is create a new function. And this function is going to be very similar to when the green fly is clicked. We're going to basically call this function to kick off our spelling game. Now I'm just going to magic some comments on the screen to give us a little bit of guidance and direction. Okay, so we need to read the lines to extract the words. And then we need to loop through each of those lines to say the words, say the sentence, spell the word, and we're going to check the result. And right at the end, we want to print the result of the game. So let's attack this first line here. We made a function in an earlier video to read the lines and extract the words and the sentences. Let's scroll up to it now. Okay, here is that function read lines from files. So I'm just going to copy it. It takes a parameter of the file name. But if we don't supply one, it's just going to default to this. So that's nice and neat. Now we need to check down what it is going to return. It returns a tuple of words and a tuple of sentences. So it's going to create two tuples there for us inside one tuple, a nested tuple. Okay, we're just going to keep that in mind. We're basically going to do this multiple assignment here. And we're going to do that down into the function that we just created. So let's scroll back down. Okay, let's paste the name of that function. Remember, we don't need to pass in anything to it because as we can see by our type int here, we've got the file name equal to lines.text. I don't need to change that. Now let's do our multiple variable assignment. We're going to assign our words and sentences to the result of read lines from file. Now, if you're a little bit uncertain about this, let's just test that it's working to do that. We can run our project here and in the console, let's just call the function start spelling and it's going to run the read lines from file and then we'll print the result of words to the console here. And there you go. You can see our words and you can also see here the sentences. They just added in that print statement. So that's working as we'd expect. Okay, let's just clear that and get rid of those prints. Okay, let's check out this next line where we're going to loop through each line. Well, we're actually just going to loop through each word in this list. And the way to do our looping is to use a for loop. But before we do that, I just want to revisit how we can access items in a list or a tuple using some notation. So you'll recall that I've got a hard coded list up the top called lines just down here in the console. You can see if I type lines and hit enter, it just returns that list and just scroll to the top of this file. If you're not sure what that is. Now the way that we can access the individual items, this list, you can see here we've got string values. The way that we do that, I'll just clear the console. So you can see what's going on is we type in the variable name. So lines and then we use that square bracket notation. If I put an integer inside of that and we start at zero, that's going to refer to the first item in the lines list here. And there you go. It prints the first line. Should you try and make a dash for the car? I can change that index and we'll get the next item. If I put a negative one here for Python, that actually means we'll get the last item in that list, which is lunch. Sometimes Alex comes home for lunch and we eat together. So the key takeaway from this is we can supply an integer number or an index to reference a particular item in that list. And over here in Scratch, we've got a block that that replicates. All that translates to and that's the item number of lines. That's exactly what we're doing. We're just doing that using the square bracket notation. Can you see that? Can you see how we're just doing the same thing there? So then what we can do is we can find the length of lines and we know how to do that. We know how to find the length of lines. Remember, we can use the function Len to do that. So if we get the function Len, we're no longer focused on lines. We've got some words and sentences here. So let's get the length of the words here. Now this is just going to count the total number of words, which is 10. And what we want to do is create an iterator. We're going to create some kind of counter variable that's going to count up from 0 to 9. So it's going to cycle through each index in our words list. And the way that we do that is we need to wrap this Len function, the length in another function called range. And the range function, what it does, is it has a starting value and an ending value and it creates all those values between them. So I've sort of created our full statement a little bit backwards here. The way you would do it is for the local variable, and I'm going to use the variable name i here just to stand for index. It's just common programming practice. You know that we've done that in our Scratch project as well. We did an underscore i and I'll just bring that up on the screen so you can see that. Okay, down here in our set column position, we've got the variable underscore i here and you see how we're setting it to 0 and we're iterating. We're changing it by 1. And that's what this for loop does. It starts at 0 and it counts all the way up to the maximum number. Okay, so we go for that variable i in this range, okay? For i in this range, we're going to do some business. And I just want to show you what's going on here. Let's just print the value of i. All right, let's clear the console, run the program, we'll call start spelling and what do you think you'll see here? Pause the video and have a guess. If you guessed all the integer values from 0 to 9, you're right. Okay, so we are just counting up from 0 to 9. I encourage you to check out the documentation for range. I'll leave a link down in the description for you to go suss out if it's a little bit confusing for you. Okay, so now we have this variable i. We need to do something with it. Well, I propose what we should do is let's create a variable called word and let's grab the current word. That's what this is going to do, okay? We're going to get the current word and while we're at it, we may as well get the sentence too. So we're going to get the current word and sentence from the two lists that we created from the read lines from file, okay? We can do that because we're iterating. So when i is equal to 0, we'll get the first word and the first sentence. When i is equal to 1, we'll get the second and so on right up until the end. So that is how this for loop works. And just to demonstrate this in action again, I've got our two print statements. It's really good to do this just to test while you go along so you don't get creating too far ahead and you encounter bugs. So you can see here we've got dash and should you try to make a dash for the car, we've got some nice pairings going on without word and sentence. Okay, I'm just going to delete those two print statements. Okay, so we did all the hard work in previous videos where we implemented say word. So all we need to do is call say word and pass in that word that we have just created here. That is why we created that parameter before and after we say word then we want to say a sentence. And what do we want to pass into sentence? You guessed it. We want to pass in the sentence and then we want to ask the user to spell the word. Okay, let's scroll up and take a look at spell word. Okay, so here's the ask the user to spell the word. It takes a word and also returns the answer. So what we want to do is store this answer in a local variable in the start spelling function. So let's go back down to it. Okay, so we need to create a local variable here called answer. And we're going to store whatever input we get from the user because then we're going to check the result. And when we check the result, you can see here that we need the answer as the first parameter. And we also need the word. We need to compare the word and the answer together. Let's just scroll up to check result. So you can see here we get the answer and we get the word. We can pair them and then we return what the result is. The result is either a zero or one. Now this is handy because we can actually keep score with this. So let's jump back down in order for us to keep score. We need to have a score variable. And you know what? I think we should store all the scores in a list here. And so what we can do is add the result to our score list here. And that's the same thing as just adding a thing to the list here in scratch. I don't think I went over that in previous videos. But yeah, when we use that append for a list, we're just adding a thing to list. So here we're going to go score dot append. And that append function just adds the thing inside of this. The thing is the result here, okay? So we're counting the score doing that. And then we want to print the result of the game. The way to do that is just do it inside of a print function here. Just follow along with me and there's I'm not going to explain it. Just put an F. We're going to go double quotes here. I'm going to start typing U spells because this is a string. I'm going to put a space and put some curly braces in. And then we're going to call a function called sum. And we're going to sum the score. And what this is going to do is take all the one values inside of our score list and just add them all together. So that's a really neat function there. And I'm just going to put a forward slash. I didn't give it a space. Space after that we're going to go more curly braces. And then we're going to call length of words. Because we want to find what the total number of words were. So we're going to take the score that we got out of the words in that list. And that's how we can print the result. These curly braces allows to put some dynamic values in there such as calculating the score or putting in the length of the words. There's a few other ways to do this. I'll put a link down description for you to go and check out. Okay, we've just about got a game here albeit in its rudimentary form. Let's run the program here and to get things rolling. What we need to do is call start spelling. When we call start spelling we're going to read the lines in the file and we're going to start iterating over the words in that list. So here we go. Dash is our first word. Here's our sentence. Should you try and make a dash for the car? And spell the word you heard. Well, we haven't heard yet but this is the prompt for us to spell that word. Correct. Great. We get that feedback. Next word is bath. Take a nice warm bath and relax for a while. Now you notice a bit of a problem here because we can just look at the word that's on the screen. So we need to also clear the console to be able to do that. Let's just scroll up above the start spelling function here and create a new function. And we're going to call it clear console. We're not going to pass that anything. I'm not going to explain this function in great detail because I googled it myself. We are going to call OS here. So I need to import a module just in a second called system. We're going to call the function that system and inside of here. We're going to return the value CLS. If the OS dot name is equal to NT else, we're going to clear and you can see we've got a little red squiggly line under that we need to scroll to the top do that now. And now we need to import OS for operating system. Now let's scroll back down and you can see our red squiggly line has gone. So now all we need to do is call clear console. We don't have to write this gnarly long line all the time. And when do we need to do that? Well, we probably want to clear the console after we've said the sentence. So let's go up to check sentence or say sentence here. And here we've got sleep. So probably the best time to do it is just before we ask the user to spell the word. So let's do it then. Let's clear the console and we ask the user to spell the word. Let's run the program and then we can call start spelling. There we get we have dash. Should you try and make a dash for the car? And look, it clears it. So that's great. We can put in the word dash and correct. Okay, so I'm just going to go through and play this game until we get the result down in the bottom here. So I will magic all of that. Okay, I'm here on the last word lunch and I'm going to type that in. Boom, correct. And you spell 10 out of 10. I also noticed a slight little bug as well. I wouldn't call it a bug, but as soon as we get the result we check the result. It says the word too quickly. So I think that we should probably add a sleep in here as well to check result. Just get a little bit of feedback on the screen. Let's just sleep for a second. Okay, and that's pretty much our spelling game. All that's left for us to do is to add in the text to speech engine. We can't actually run that program here in replete which is why we've left it to the end. But we're going to code it in here first and then I'm going to show you how to run it on your local machine because you will need to install Python locally. So we've got two more videos left in this series. You've done an incredible job. If you've come all this way, you've really started to understand some core concepts of Python. So keep at it. You're nearly there. We've got a couple of videos left. I can't wait to round up this series with you. But until next time, I'm off to go find a wave and I'll catch you in the next one.