 In this tutorial we're going to translate one about final custom blocks here, extract words and sentences from lines. Remember right at the start of this series we created a constant list here of all of those lines. In the next video we're going to import a text base list but for now we're just going to use this list. We're going to introduce a new looping mechanism in this tutorial and it's called using a for loop. We're going to iterate or loop over each item in this list to read the lines. So let's scroll down and get stuck into it. 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 out from Scratch to Python series where we're trying to dip your toes in the deep end of a text-based programming language and that one is Python. We've created a project in Scratch, a spelling game and we're trying to translate it across into Python. We're about five or six videos into this series code so go check the card in the top right hand corner to check out some early videos or grab a link to the starter project down in the description. First thing that we need to is create our function definition of extracting the word and sentences from lines. So I'm just going to make that a plural and this is going to be a function that we're not going to pass any parameter here and don't forget that colon. Okay so here over in Scratch we've got two lists that were created. We've got sentences and words and we delete them all at the start and the way to do that, the way to do that in Python is to simply just create some new lists here. We can create a new list by just using this square bracket notation. Another way that you could initialize that is just by calling the list constructor here. So this is like a list function and this is just going to create a new list. It really doesn't matter which one you use and sometimes you want to convert things to lists and you want to use this list constructor but for the simplicity I'm just going to create two square bracket new lists here. Now imagine this a couple of times I've said that these variables are local variables. There are also variables called global variables. Now one on earth is the difference between these two. I'm going to click the green play button here. I nearly said flag but you'll see that since I've clicked this and if I type in words you might think oh yeah we're going to get a reference to this list here but in fact we don't. It's telling us here that words is not defined. That's a bit of a problem. Why is that the case? Well the reason for that is the word kind of lives in the function here or doesn't kind of it lives in the house of this function and if we try and reference anything inside the house well we can't get to it because it's locked in there. When we're in the console we're out here in the street and we're trying to access these things on the house and currently we don't have any way to do it. If we wanted to access words we would need to create a words variable outside of here okay and then inside of this local function we would need to put this keyword global and say words. Now this list refers to the global list outside of here. You can see that when I get rid of the word global we've got a green little squiggly line under our words here and it's telling us that this variable is no longer being used. If I click the run button again and now I refer to that words list you'll see that we're getting that empty list and just to really underscore the difference between these two I'm going to put a value inside of this list and I'm going to call it global. I'll put another value inside of this list and I'm going to call it local. I'm going to hit the run button there. I'm going to access that words and you see that we're getting the global list here. Now inside of our function here if I put the keyword global and a reference to the variable that I want to use that is global any reference going forward is now going to reference the global variable. So what do you think will happen now as we run this project and I type in words? What value do you think we're going to get here? We still get global. What's going on here? The reason is we haven't actually called this function yet so we haven't changed the value of words. So let's go ahead and call the function. It'll do its work. Now we'll find out what the reference to our words is here and you can see we've changed it to local. So we've actually gone in and we've changed that global variable to be assigned to this new list here. So that's just a little quick crash course on the difference between local variables and global variables. I'll leave a link in the description for a little bit more reading on that and there's probably a super video that explains the difference between these two I'll link to in the top right hand corner as well. But for now I'm going to get rid of our global definitions here and we're going to reset this back to an empty list. Okay let's jump on to the next part of this tutorial and we're going to be introduced to for loops. What is a for loop? Well a for loop means that we just iterate over something and the thing that we're going to iterate over is that list of lines that we have up the top. The way that a for loop works is we can type the keyword for and the next piece of the syntax is the item in the list right and the item for us is going to be a line. So for the line inside the list of lines now what does this refer to up here? Well if we scroll up the top you can see that we've got a global constant list of all these individual lines so if we scroll down a for loop is simply just going to iterate over each of these lines and of course we need a colon character there at the end. And if we jump over to scratch now you can see that we've kind of replicated what's going on here. We've got a variable here calling line num and we've got this repeat and we're repeating for the length of the lines. Well that's what this for loop is going to do as well as saying all right for the line in lines and it's saying all the lines so that's what we're doing here repeating something for the length of lines and what we want to repeat is the split line functionality that we determined up here. So we can get our multiple variable assignment and get our word and sentence here we call that split line function that we created earlier and we can pass it the line here. So in all the references to line that we've been using for all of our functions so far this is where it starts this is the top of the tree or the bottom depending on how you want to look at it this is where we're getting that line and we're feeding it to all the functions that depend on it. And remember in the last tutorial when we have this split line function it's returning a tuple of a word and a sentence and a tuple is just a list that we cannot change okay. We know that the first value of that tuple is going to be the word and the second value is going to be sentence. So we can use that multiple variable assignment there to help us out that we chatted about in that last video. Okay to really help visualize what on earth is going on here let's jump over to python tutor to check it out. Okay I'm over here in python tutor link in the description if you want to go suss it out as well. Let's visualize this execution you can see I've just got our list of lines here. I can step through this and it's basically just going to encode all their lines so I'm going to just run through all of that. Now we've started here at our full loop and this is where it all gets interesting. Okay so it's executed the first line here and we've printed nothing so let's jump into it and it's going to print the first line in line so see here this is our lines constant list that we are referring to and it's getting the first line it's iterating over the first line let's click next and we have just printed that output to the screen so we've just printed that line now it's going to go get the next line in that list and the next line is barth you can see that then we're going to print barth let's get the next line it's going to be apologize and we'll print apologize to the list and so forth see how it's just stepping over each of these lists and that's what a full loop does it just says all right let's go through and cycle through all of these values. Now there's a couple of other ways that we could do this but I'm conscious I don't want to bust your brain here I will leave a link to the description for for loops and iteration down below so go and check that out as well. Okay let's jump back across to replete the next thing we want to do is do something with this word and sentence that we're getting we just don't want to print it to the console what we want to do is insert it inside of these lists because back over here in scratch that's what we're doing we're adding the output of the word to that list and in the last tutorial I said that yeah we don't want to do that inside of the extract word function because it's just a bit messy because that function extract word from line we just wanted to do that one thing we don't want it to do too much all right here we're extracting the words and sentences from line so there's plural going on here we're extracting it and then we're adding them to the words and sentences so let's go ahead and do that you need to stay on the line of indentation for this for loop and we want to refer to the words list and you can see we're now referring to it that green squiggly line just went away now there's a word called a pend and the word append here is a function on a list and we can just add whatever we want to it onto the end of the list we'll append it onto it and we're going to append the word we can do the same thing for our sentences let's append the sentence that we just extracted so you got a little typo there let's put our dot syntax back there and just to finish this up we can return our words and our sentences and this will return those two lists if I click the run button here and we just call that function with any luck we'll see two lists one of words and one of sentences great now that's a little bit hard to see I'm just going to quickly import a python library just to clean this up so you can see it a little bit neater okay I've just gone ahead and imported a module called pprint and it stands for pretty print and I'm just going to call pprint dot pprint and then we can just pass in that function that we created if I scroll down we're going to get the words and the sentences and it's going to handle it for us and now let's just give yourself a little bit of space and you can see that we've just returned a list you can tell that by the opening of the square bracket and the closing there's our list of our words and a list of our sentences here and you can see that we've just got a little space there I'm not going to worry too much about that we can strip that out but I'll let you google that how do you strip a white space character from a string well awesome job on making it through this tutorial we've essentially got all the functionality that we need to get our text to speech working here but before we do that there's something wrong with our project we're still using a hard coded list and we want to go through and create that text file that we've been using in our scratch project we want to utilize that because we only want to create the thing once and that's what we'll do in the next tutorial we're going to implement this read lines functionality and interestingly enough I'm going to show you a couple of one-liners that is pretty much going to do everything that we've done in one function here which is going to be pretty gnarly stuff so make sure you stay tuned for that one but until then I'm off to go outside enjoy this gorgeous weather find a wave and I will catch you in the next one