WEBVTT

0
00:00.390 --> 00:03.150
All right. So now that you've learned about functions,

1
00:03.300 --> 00:07.680
I have a coding challenge for you. If you head over to this link,

2
00:07.800 --> 00:12.300
which is in the course resources and head over to this challenge

3
00:12.300 --> 00:13.710
which is called Hurdle 1,

4
00:14.310 --> 00:19.310
the idea is that we've got a robot here which needs to do a number of hurdles to

5
00:19.590 --> 00:23.190
jump over each of these barriers, to get to the final goal here.

6
00:23.820 --> 00:27.900
Now you need to use your knowledge of functions

7
00:28.140 --> 00:30.810
as well as some of the things you've learned before, such as the

8
00:30.810 --> 00:34.920
for loop or the range function, to be able to achieve this.

9
00:35.640 --> 00:39.810
Now, remember that you can tell the robot to move by typing, move(),

10
00:40.290 --> 00:43.350
and when that's run, it will move by one space.

11
00:43.890 --> 00:46.200
And you can also use, say,

12
00:46.230 --> 00:50.160
turn_left to get the robot to turn left.

13
00:50.700 --> 00:52.320
And in this case, when I run the code,

14
00:52.320 --> 00:55.530
it's going to move one space and face to the left.

15
00:56.640 --> 00:59.130
In order to complete this entire challenge,

16
00:59.190 --> 01:03.030
you can see there's a number of steps that the robot has to go through before it

17
01:03.030 --> 01:07.830
can get to this final goal. And if you were to write out each of these steps,

18
01:08.160 --> 01:12.360
like I have done here one after the other, it would take many,

19
01:12.390 --> 01:16.950
many tens of lines of code. And that's not what we're about. We're programmers.

20
01:16.980 --> 01:18.660
So we're born to be lazy.

21
01:19.080 --> 01:23.400
Try and see if you can minimize number lines of code while still keeping your

22
01:23.400 --> 01:28.400
code readable and understandable by somebody else and be able to get your robot

23
01:28.710 --> 01:33.300
to complete these instructions and take the robot to the final goalpost.

24
01:34.140 --> 01:36.930
And once you've completed the challenge successfully,

25
01:37.200 --> 01:42.200
you should get a green popup that says you're at the correct X and Y position.

26
01:42.990 --> 01:44.520
So pause the video now,

27
01:44.550 --> 01:48.150
head over to this link and give this challenge a go.

28
01:48.390 --> 01:49.223
<v 1>Right.</v>

29
01:54.290 --> 01:57.050
<v 0>All right. So how did you get on with this challenge?</v>

30
01:57.830 --> 02:02.720
How many lines of code did it take you to tell this robot to get to the goal?

31
02:03.560 --> 02:06.230
All right. Let's have a think about how we might tackle this.

32
02:06.500 --> 02:10.460
We know that out of the things that we can tell the robot to do, the most useful

33
02:10.460 --> 02:13.190
things are to move and to turn left.

34
02:13.670 --> 02:17.480
But because the robot doesn't have a builtin turn right function,

35
02:17.720 --> 02:19.820
then we have to define that ourselves.

36
02:20.360 --> 02:23.750
And the reason why we need to do that is because you can see that at several

37
02:23.750 --> 02:27.920
points during the hurdle, we'll have to turn right. For example,

38
02:27.920 --> 02:32.570
when the robot is here at 2, 2, it will have to turn right,

39
02:32.630 --> 02:35.810
and then move and then turn right again and then move.

40
02:36.290 --> 02:39.650
So let's create a new function called turn_right.

41
02:40.190 --> 02:44.990
So we start out with the def key keyword and then give our function a name which

42
02:44.990 --> 02:46.280
is going to be turn_right.

43
02:47.270 --> 02:50.180
And then we add the parentheses with nothing inside

44
02:50.210 --> 02:52.880
because we're not passing any inputs to this function.

45
02:53.360 --> 02:58.360
We're just going to use this function as a way of defining turn left three

46
02:59.350 --> 03:03.970
times. So when you turn left three times, of course you turn right.

47
03:04.480 --> 03:07.330
And now we have the ability to turn right.

48
03:07.870 --> 03:12.280
So notice how this is the code block for our turn right function

49
03:12.730 --> 03:16.690
and it ends at the last line, which is indented, which is line 4.

50
03:17.230 --> 03:19.030
So every subsequent line

51
03:19.060 --> 03:22.560
which starts at the very beginning of the code file next to

52
03:22.580 --> 03:27.430
the left margin is outside this code block and it will act independently.

53
03:28.000 --> 03:28.810
So in fact,

54
03:28.810 --> 03:33.430
the first line of code that actually runs in our file is going to be this line

55
03:33.430 --> 03:36.760
6. And you can see that when I press run,

56
03:36.820 --> 03:39.370
you can see that's the first thing that gets highlighted.

57
03:40.030 --> 03:42.340
And if I go through this step wise,

58
03:42.400 --> 03:47.260
you can see the first line is going to be line 6 and it tells the computer to

59
03:47.260 --> 03:51.520
go and find this function called turn_right. So if I skipped to the next step,

60
03:51.700 --> 03:55.930
it's found that, and it's going to start going through lines 2, 3,

61
03:55.930 --> 03:56.763
and 4,

62
03:57.040 --> 04:02.040
turning our robot three times until it has turned right. Now that we have the

63
04:03.970 --> 04:05.440
ability to turn right,

64
04:05.560 --> 04:10.560
let's have a think about how we might be able to get our robot to make one jump.

65
04:11.440 --> 04:16.440
Let's see. The first thing we'd probably want our robot to do is to move forwards

66
04:16.570 --> 04:19.120
by one step, which should take it here.

67
04:19.630 --> 04:24.630
And then the next thing we want it to do is the turn left so that it faces the

68
04:24.760 --> 04:25.593
top.

69
04:28.680 --> 04:28.940
<v 1>Right?</v>

70
04:28.940 --> 04:31.220
<v 0>And once it's in this position,</v>

71
04:31.250 --> 04:34.580
then we want to get it to move forward one step,

72
04:35.210 --> 04:39.920
and then we'll probably want it to turn right. Now,

73
04:39.950 --> 04:44.950
notice how I'm actually testing my code at pretty much every other line that I

74
04:45.260 --> 04:46.093
write.

75
04:46.400 --> 04:50.570
And this means that you don't end up in a situation where you get to the end and

76
04:50.570 --> 04:54.020
you've written lots and lots of lines of code, and it doesn't work.

77
04:54.530 --> 04:58.430
In which case you have to comb through all of the code you've written and find

78
04:58.430 --> 05:01.190
out where the problem is. So in this case,

79
05:01.220 --> 05:04.310
we've only got four lines and we've been testing it step wise.

80
05:04.700 --> 05:09.020
So we can see that we're on track and our robot is now turned to the right,

81
05:09.350 --> 05:11.210
ready to move one more step.

82
05:13.670 --> 05:15.350
And then once it gets to here,

83
05:15.350 --> 05:18.410
we have to turn right again and move one more step.

84
05:23.980 --> 05:24.280
<v 1>Right?</v>

85
05:24.280 --> 05:27.910
<v 0>And now it should end up here, but let's just check it out.</v>

86
05:28.630 --> 05:32.230
So our robot turns left turns right, turns right again.

87
05:32.650 --> 05:35.110
And it ends up here facing the bottom.

88
05:35.710 --> 05:40.480
So the final thing we need to do is to get it, to turn left once more,

89
05:42.010 --> 05:44.350
so that it will face the forward direction.

90
05:47.630 --> 05:47.900
<v 1>Right?</v>

91
05:47.900 --> 05:50.750
<v 0>So once you've confirmed that your code is working,</v>

92
05:51.230 --> 05:54.110
then you should really think about what is the next step,

93
05:54.140 --> 05:57.950
because we've managed to make one hurdle. And notice how,

94
05:57.980 --> 06:01.700
when we're at this position, 3,1, this particular square,

95
06:02.120 --> 06:05.000
it's almost the same as if we were at 1, 1,

96
06:05.120 --> 06:10.040
because we have to move one step again, turn left, move, turn right, move,

97
06:10.070 --> 06:12.080
turn, right, move, turn left.

98
06:12.380 --> 06:17.380
So it's basically repeating all of these instructions, rather than executing

99
06:18.140 --> 06:22.490
these instructions six times, because there are six hurdles.

100
06:23.120 --> 06:25.430
So if I go back and I start,

101
06:25.520 --> 06:28.910
then you'll see that this code actually will complete the challenge.

102
06:31.980 --> 06:32.813
<v 1>Right?</v>

103
06:32.820 --> 06:35.790
<v 0>It says I'm done. And we're at the correct position,</v>

104
06:35.940 --> 06:38.160
but notice how many lines of code we've got.

105
06:38.160 --> 06:41.340
We've got something like 58 lines of code.

106
06:41.400 --> 06:44.700
I know I've left some spaces in their so that you can see which parts are

107
06:44.700 --> 06:49.290
repeating, but still that's a massive amount of code for something very simple.

108
06:49.860 --> 06:53.040
Now we know that we can use functions to package

109
06:53.100 --> 06:55.680
a set of instructions together under one name,

110
06:56.040 --> 06:58.560
just like what we've done here with the turn_right.

111
06:58.920 --> 07:02.010
We've packaged the three lines of code turn left, turn left, turn

112
07:02.010 --> 07:05.040
left into a single function called turn_right

113
07:05.460 --> 07:09.930
so that when we need that functionality, all we need to do is just the call

114
07:09.930 --> 07:12.360
the function and add the parentheses at the end.

115
07:12.960 --> 07:17.550
So we can do exactly the same thing with this set of instructions,

116
07:17.610 --> 07:21.360
which basically gets our robot to perform a single jump.

117
07:22.050 --> 07:25.260
So let's create a def and let's call this function jump.

118
07:25.920 --> 07:30.420
And then lets add the parentheses and the colon. And very importantly,

119
07:30.450 --> 07:34.590
all of these lines of code must be indented. So to indent

120
07:34.590 --> 07:36.090
a whole block of code together,

121
07:36.330 --> 07:41.330
you hold down the command key and click the left square bracket or the right

122
07:41.460 --> 07:45.600
square bracket. On windows it's holding down the control key and again,

123
07:45.600 --> 07:48.450
the last square bracket or the right square bracket.

124
07:49.140 --> 07:54.140
So now this basically says that all of these instructions live inside this block

125
07:54.660 --> 07:58.890
of code called jump. And when we call jump,

126
07:59.760 --> 08:03.300
then it will carry out all of these lines of code.

127
08:04.680 --> 08:09.680
So now what we could do to complete the challenge is to simply call this

128
08:09.810 --> 08:11.610
function jump six times.

129
08:12.690 --> 08:13.523
<v 1>Right?</v>

130
08:14.740 --> 08:19.740
<v 0>And so now we could again solve this entire problem using just 21 lines of code.</v>

131
08:23.050 --> 08:28.050
And we managed to complete this challenge using two functions that have repeated

132
08:28.720 --> 08:32.560
instructions and then calling the jump function six times.

133
08:33.130 --> 08:37.750
But because we've learned about loops and the range function,

134
08:38.110 --> 08:41.350
we know that we can actually cut this down even shorter.

135
08:41.890 --> 08:44.290
So instead of calling jump six times,

136
08:44.320 --> 08:49.320
we could actually write a for loop that loops through and calls this jump

137
08:50.290 --> 08:50.860
function

138
08:50.860 --> 08:55.860
six times. We could say something like for step in,

139
08:56.880 --> 09:00.090
and remember that these two key words come from the

140
09:00.090 --> 09:02.730
for...in loop and they always have to stay the same.

141
09:03.480 --> 09:05.280
And then after the in keyword,

142
09:05.400 --> 09:09.600
we define the rules for how many times we want our loop to repeat.

143
09:10.050 --> 09:13.020
In my case, I'm going to use in range function.

144
09:13.530 --> 09:17.370
And I'm going to say from 0 to 6,

145
09:17.700 --> 09:22.380
but not including 6. So in this case it will be 0, 1, 2, 

146
09:22.410 --> 09:23.910
3, 4, 5.

147
09:24.270 --> 09:27.420
So that actually going to happen six times.

148
09:28.020 --> 09:32.070
And now I'm going to add the colon. And finally,

149
09:32.160 --> 09:33.930
inside this for loop,

150
09:33.960 --> 09:37.650
I'm going to call jump just once. Now,

151
09:37.650 --> 09:41.790
watch what happens. First it starts off at the for loop,

152
09:42.180 --> 09:47.180
it calls jump and it goes through this once and then it comes back loops again,

153
09:47.880 --> 09:51.690
calls it the next time. And then again,

154
09:51.840 --> 09:55.680
the third time in the loop, fourth time in the loop,

155
09:56.280 --> 09:59.520
fifth time in the loop and six time in the loop.

156
10:01.880 --> 10:03.500
How far did you manage to get?

157
10:03.950 --> 10:08.630
Did you get stuck on this section where we needed to create the for loop?

158
10:09.200 --> 10:10.610
Well, in that case,

159
10:10.640 --> 10:14.840
I recommend going back to the lesson where we covered for loops and reviewing

160
10:14.840 --> 10:19.520
the for loop and the range function in detail before you come back and try to

161
10:19.520 --> 10:21.980
solve this challenge again. It's really,

162
10:21.980 --> 10:26.600
really important at this stage that you've understood and you've mastered some

163
10:26.600 --> 10:31.250
of these ideas because we're going to be using them more and more in the future.

164
10:31.910 --> 10:35.360
Really make sure that you've understood things before you keep going.