From the course: Python: Recursion

Recursion in action - Python Tutorial

From the course: Python: Recursion

Start my 1-month free trial

Recursion in action

- [Instructor] We've looked at the first two ingredients of a recursive algorithm. That's the base case and movement towards the base case. Now we're going to look at the third ingredient. That's the recursive call itself. We're going to do this in the context of a turtle graphics program which draws a spiral. This is the same program we saw earlier. So first of all, I'm going to run this. So to follow along, you're going to need to open up Spiral_turtle.py in chapter O two O two. And if you're using Git then, do ALT F12 and you need to do Git Checkouts O two underscore O two. And there's no B or E because I'm not going to change any code in this chapter. Okay so I'm already on that branch. So then let's run it and see what happens. Okay, so it does in fact stop, which is a good thing. Let's see how it works. So the base case is where we have this line length and we compare it to the max length. So max length is a constant that we set at the top of our file on line eight. And we said 250, okay? So that is now our base case. So if the line length becomes greater than that then we do nothing. We return, and the whole thing starts to unwind. And we'll talk about winding and unwinding later on but that's our base case, okay? So do we have movement towards the Base Case? Well, if we look at line 17 this is actually the third ingredient. This is the recursive call itself. You see the function, which is being called is exactly the function which we are currently defining. Okay, so this is the recursive call. You're calling yourself within the function. Okay, and we're passing in the line length plus an increment. Now the increment, again, is defined as a constant at the top of the file. So we have a situation where each time we recursively call draw a spiral, we pass in an increased line length. And then at the beginning of each time through the function we make that comparison we say has the line length exceeded our base case? And if it has, then we return. Otherwise, we do the drawing that's a turtle forward and turtle right And then we call the function again. So the key here line 17 is your recursive function call, and it has an altered parameter from the one that it was originally called with. And that enables movement towards the base case. What I recommend you do with this program is make a few changes. Try changing the maximum length, try changing the increment. You can also mess around with some of the properties like the color of the turtle and the speed and the pen size there. I haven't done anything for speed but if you want to do speed, you can do Charlie.speed. Charlie is just an arbitrary name that I've given to the particular turtle that we're using. You could call it anything you'd like, but yeah I always encourage you with any code that I give you to play around with it change some things don't worry about breaking it. You can always use git to get back to where you were or if you need to make a copy of it. If you're worried about losing the original you can make a copy of it but that's a really a good way to kind of consolidate your learning is to change things. Break it, fix it again, make it your own. That way you'll deepen your understanding.

Contents