All right. So now that you've learned about functions,
I have a coding challenge for you. If you head over to this link,
which is in the course resources and head over to this challenge
which is called Hurdle 1,
the idea is that we've got a robot here which needs to do a number of hurdles to
jump over each of these barriers, to get to the final goal here.
Now you need to use your knowledge of functions
as well as some of the things you've learned before, such as the
for loop or the range function, to be able to achieve this.
Now, remember that you can tell the robot to move by typing, move(),
and when that's run, it will move by one space.
And you can also use, say,
turn_left to get the robot to turn left.
And in this case, when I run the code,
it's going to move one space and face to the left.
In order to complete this entire challenge,
you can see there's a number of steps that the robot has to go through before it
can get to this final goal. And if you were to write out each of these steps,
like I have done here one after the other, it would take many,
many tens of lines of code. And that's not what we're about. We're programmers.
So we're born to be lazy.
Try and see if you can minimize number lines of code while still keeping your
code readable and understandable by somebody else and be able to get your robot
to complete these instructions and take the robot to the final goalpost.
And once you've completed the challenge successfully,
you should get a green popup that says you're at the correct X and Y position.
So pause the video now,
head over to this link and give this challenge a go.
Right.
All right. So how did you get on with this challenge?
How many lines of code did it take you to tell this robot to get to the goal?
All right. Let's have a think about how we might tackle this.
We know that out of the things that we can tell the robot to do, the most useful
things are to move and to turn left.
But because the robot doesn't have a builtin turn right function,
then we have to define that ourselves.
And the reason why we need to do that is because you can see that at several
points during the hurdle, we'll have to turn right. For example,
when the robot is here at 2, 2, it will have to turn right,
and then move and then turn right again and then move.
So let's create a new function called turn_right.
So we start out with the def key keyword and then give our function a name which
is going to be turn_right.
And then we add the parentheses with nothing inside
because we're not passing any inputs to this function.
We're just going to use this function as a way of defining turn left three
times. So when you turn left three times, of course you turn right.
And now we have the ability to turn right.
So notice how this is the code block for our turn right function
and it ends at the last line, which is indented, which is line 4.
So every subsequent line
which starts at the very beginning of the code file next to
the left margin is outside this code block and it will act independently.
So in fact,
the first line of code that actually runs in our file is going to be this line
6. And you can see that when I press run,
you can see that's the first thing that gets highlighted.
And if I go through this step wise,
you can see the first line is going to be line 6 and it tells the computer to
go and find this function called turn_right. So if I skipped to the next step,
it's found that, and it's going to start going through lines 2, 3,
and 4,
turning our robot three times until it has turned right. Now that we have the
ability to turn right,
let's have a think about how we might be able to get our robot to make one jump.
Let's see. The first thing we'd probably want our robot to do is to move forwards
by one step, which should take it here.
And then the next thing we want it to do is the turn left so that it faces the
top.
Right?
And once it's in this position,
then we want to get it to move forward one step,
and then we'll probably want it to turn right. Now,
notice how I'm actually testing my code at pretty much every other line that I
write.
And this means that you don't end up in a situation where you get to the end and
you've written lots and lots of lines of code, and it doesn't work.
In which case you have to comb through all of the code you've written and find
out where the problem is. So in this case,
we've only got four lines and we've been testing it step wise.
So we can see that we're on track and our robot is now turned to the right,
ready to move one more step.
And then once it gets to here,
we have to turn right again and move one more step.
Right?
And now it should end up here, but let's just check it out.
So our robot turns left turns right, turns right again.
And it ends up here facing the bottom.
So the final thing we need to do is to get it, to turn left once more,
so that it will face the forward direction.
Right?
So once you've confirmed that your code is working,
then you should really think about what is the next step,
because we've managed to make one hurdle. And notice how,
when we're at this position, 3,1, this particular square,
it's almost the same as if we were at 1, 1,
because we have to move one step again, turn left, move, turn right, move,
turn, right, move, turn left.
So it's basically repeating all of these instructions, rather than executing
these instructions six times, because there are six hurdles.
So if I go back and I start,
then you'll see that this code actually will complete the challenge.
Right?
It says I'm done. And we're at the correct position,
but notice how many lines of code we've got.
We've got something like 58 lines of code.
I know I've left some spaces in their so that you can see which parts are
repeating, but still that's a massive amount of code for something very simple.
Now we know that we can use functions to package
a set of instructions together under one name,
just like what we've done here with the turn_right.
We've packaged the three lines of code turn left, turn left, turn
left into a single function called turn_right
so that when we need that functionality, all we need to do is just the call
the function and add the parentheses at the end.
So we can do exactly the same thing with this set of instructions,
which basically gets our robot to perform a single jump.
So let's create a def and let's call this function jump.
And then lets add the parentheses and the colon. And very importantly,
all of these lines of code must be indented. So to indent
a whole block of code together,
you hold down the command key and click the left square bracket or the right
square bracket. On windows it's holding down the control key and again,
the last square bracket or the right square bracket.
So now this basically says that all of these instructions live inside this block
of code called jump. And when we call jump,
then it will carry out all of these lines of code.
So now what we could do to complete the challenge is to simply call this
function jump six times.
Right?
And so now we could again solve this entire problem using just 21 lines of code.
And we managed to complete this challenge using two functions that have repeated
instructions and then calling the jump function six times.
But because we've learned about loops and the range function,
we know that we can actually cut this down even shorter.
So instead of calling jump six times,
we could actually write a for loop that loops through and calls this jump
function
six times. We could say something like for step in,
and remember that these two key words come from the
for...in loop and they always have to stay the same.
And then after the in keyword,
we define the rules for how many times we want our loop to repeat.
In my case, I'm going to use in range function.
And I'm going to say from 0 to 6,
but not including 6. So in this case it will be 0, 1, 2,
3, 4, 5.
So that actually going to happen six times.
And now I'm going to add the colon. And finally,
inside this for loop,
I'm going to call jump just once. Now,
watch what happens. First it starts off at the for loop,
it calls jump and it goes through this once and then it comes back loops again,
calls it the next time. And then again,
the third time in the loop, fourth time in the loop,
fifth time in the loop and six time in the loop.
How far did you manage to get?
Did you get stuck on this section where we needed to create the for loop?
Well, in that case,
I recommend going back to the lesson where we covered for loops and reviewing
the for loop and the range function in detail before you come back and try to
solve this challenge again. It's really,
really important at this stage that you've understood and you've mastered some
of these ideas because we're going to be using them more and more in the future.
Really make sure that you've understood things before you keep going.