1
00:00:00,390 --> 00:00:03,150
All right. So now that you've learned about functions,

2
00:00:03,300 --> 00:00:07,680
I have a coding challenge for you. If you head over to this link,

3
00:00:07,800 --> 00:00:12,300
which is in the course resources and head over to this challenge

4
00:00:12,300 --> 00:00:13,710
which is called Hurdle 1,

5
00:00:14,310 --> 00:00:19,310
the idea is that we've got a robot here which needs to do a number of hurdles to

6
00:00:19,590 --> 00:00:23,190
jump over each of these barriers, to get to the final goal here.

7
00:00:23,820 --> 00:00:27,900
Now you need to use your knowledge of functions

8
00:00:28,140 --> 00:00:30,810
as well as some of the things you've learned before, such as the

9
00:00:30,810 --> 00:00:34,920
for loop or the range function, to be able to achieve this.

10
00:00:35,640 --> 00:00:39,810
Now, remember that you can tell the robot to move by typing, move(),

11
00:00:40,290 --> 00:00:43,350
and when that's run, it will move by one space.

12
00:00:43,890 --> 00:00:46,200
And you can also use, say,

13
00:00:46,230 --> 00:00:50,160
turn_left to get the robot to turn left.

14
00:00:50,700 --> 00:00:52,320
And in this case, when I run the code,

15
00:00:52,320 --> 00:00:55,530
it's going to move one space and face to the left.

16
00:00:56,640 --> 00:00:59,130
In order to complete this entire challenge,

17
00:00:59,190 --> 00:01:03,030
you can see there's a number of steps that the robot has to go through before it

18
00:01:03,030 --> 00:01:07,830
can get to this final goal. And if you were to write out each of these steps,

19
00:01:08,160 --> 00:01:12,360
like I have done here one after the other, it would take many,

20
00:01:12,390 --> 00:01:16,950
many tens of lines of code. And that's not what we're about. We're programmers.

21
00:01:16,980 --> 00:01:18,660
So we're born to be lazy.

22
00:01:19,080 --> 00:01:23,400
Try and see if you can minimize number lines of code while still keeping your

23
00:01:23,400 --> 00:01:28,400
code readable and understandable by somebody else and be able to get your robot

24
00:01:28,710 --> 00:01:33,300
to complete these instructions and take the robot to the final goalpost.

25
00:01:34,140 --> 00:01:36,930
And once you've completed the challenge successfully,

26
00:01:37,200 --> 00:01:42,200
you should get a green popup that says you're at the correct X and Y position.

27
00:01:42,990 --> 00:01:44,520
So pause the video now,

28
00:01:44,550 --> 00:01:48,150
head over to this link and give this challenge a go.

29
00:01:48,390 --> 00:01:49,223
Right.

30
00:01:54,290 --> 00:01:57,050
All right. So how did you get on with this challenge?

31
00:01:57,830 --> 00:02:02,720
How many lines of code did it take you to tell this robot to get to the goal?

32
00:02:03,560 --> 00:02:06,230
All right. Let's have a think about how we might tackle this.

33
00:02:06,500 --> 00:02:10,460
We know that out of the things that we can tell the robot to do, the most useful

34
00:02:10,460 --> 00:02:13,190
things are to move and to turn left.

35
00:02:13,670 --> 00:02:17,480
But because the robot doesn't have a builtin turn right function,

36
00:02:17,720 --> 00:02:19,820
then we have to define that ourselves.

37
00:02:20,360 --> 00:02:23,750
And the reason why we need to do that is because you can see that at several

38
00:02:23,750 --> 00:02:27,920
points during the hurdle, we'll have to turn right. For example,

39
00:02:27,920 --> 00:02:32,570
when the robot is here at 2, 2, it will have to turn right,

40
00:02:32,630 --> 00:02:35,810
and then move and then turn right again and then move.

41
00:02:36,290 --> 00:02:39,650
So let's create a new function called turn_right.

42
00:02:40,190 --> 00:02:44,990
So we start out with the def key keyword and then give our function a name which

43
00:02:44,990 --> 00:02:46,280
is going to be turn_right.

44
00:02:47,270 --> 00:02:50,180
And then we add the parentheses with nothing inside

45
00:02:50,210 --> 00:02:52,880
because we're not passing any inputs to this function.

46
00:02:53,360 --> 00:02:58,360
We're just going to use this function as a way of defining turn left three

47
00:02:59,350 --> 00:03:03,970
times. So when you turn left three times, of course you turn right.

48
00:03:04,480 --> 00:03:07,330
And now we have the ability to turn right.

49
00:03:07,870 --> 00:03:12,280
So notice how this is the code block for our turn right function

50
00:03:12,730 --> 00:03:16,690
and it ends at the last line, which is indented, which is line 4.

51
00:03:17,230 --> 00:03:19,030
So every subsequent line

52
00:03:19,060 --> 00:03:22,560
which starts at the very beginning of the code file next to

53
00:03:22,580 --> 00:03:27,430
the left margin is outside this code block and it will act independently.

54
00:03:28,000 --> 00:03:28,810
So in fact,

55
00:03:28,810 --> 00:03:33,430
the first line of code that actually runs in our file is going to be this line

56
00:03:33,430 --> 00:03:36,760
6. And you can see that when I press run,

57
00:03:36,820 --> 00:03:39,370
you can see that's the first thing that gets highlighted.

58
00:03:40,030 --> 00:03:42,340
And if I go through this step wise,

59
00:03:42,400 --> 00:03:47,260
you can see the first line is going to be line 6 and it tells the computer to

60
00:03:47,260 --> 00:03:51,520
go and find this function called turn_right. So if I skipped to the next step,

61
00:03:51,700 --> 00:03:55,930
it's found that, and it's going to start going through lines 2, 3,

62
00:03:55,930 --> 00:03:56,763
and 4,

63
00:03:57,040 --> 00:04:02,040
turning our robot three times until it has turned right. Now that we have the

64
00:04:03,970 --> 00:04:05,440
ability to turn right,

65
00:04:05,560 --> 00:04:10,560
let's have a think about how we might be able to get our robot to make one jump.

66
00:04:11,440 --> 00:04:16,440
Let's see. The first thing we'd probably want our robot to do is to move forwards

67
00:04:16,570 --> 00:04:19,120
by one step, which should take it here.

68
00:04:19,630 --> 00:04:24,630
And then the next thing we want it to do is the turn left so that it faces the

69
00:04:24,760 --> 00:04:25,593
top.

70
00:04:28,680 --> 00:04:28,940
Right?

71
00:04:28,940 --> 00:04:31,220
And once it's in this position,

72
00:04:31,250 --> 00:04:34,580
then we want to get it to move forward one step,

73
00:04:35,210 --> 00:04:39,920
and then we'll probably want it to turn right. Now,

74
00:04:39,950 --> 00:04:44,950
notice how I'm actually testing my code at pretty much every other line that I

75
00:04:45,260 --> 00:04:46,093
write.

76
00:04:46,400 --> 00:04:50,570
And this means that you don't end up in a situation where you get to the end and

77
00:04:50,570 --> 00:04:54,020
you've written lots and lots of lines of code, and it doesn't work.

78
00:04:54,530 --> 00:04:58,430
In which case you have to comb through all of the code you've written and find

79
00:04:58,430 --> 00:05:01,190
out where the problem is. So in this case,

80
00:05:01,220 --> 00:05:04,310
we've only got four lines and we've been testing it step wise.

81
00:05:04,700 --> 00:05:09,020
So we can see that we're on track and our robot is now turned to the right,

82
00:05:09,350 --> 00:05:11,210
ready to move one more step.

83
00:05:13,670 --> 00:05:15,350
And then once it gets to here,

84
00:05:15,350 --> 00:05:18,410
we have to turn right again and move one more step.

85
00:05:23,980 --> 00:05:24,280
Right?

86
00:05:24,280 --> 00:05:27,910
And now it should end up here, but let's just check it out.

87
00:05:28,630 --> 00:05:32,230
So our robot turns left turns right, turns right again.

88
00:05:32,650 --> 00:05:35,110
And it ends up here facing the bottom.

89
00:05:35,710 --> 00:05:40,480
So the final thing we need to do is to get it, to turn left once more,

90
00:05:42,010 --> 00:05:44,350
so that it will face the forward direction.

91
00:05:47,630 --> 00:05:47,900
Right?

92
00:05:47,900 --> 00:05:50,750
So once you've confirmed that your code is working,

93
00:05:51,230 --> 00:05:54,110
then you should really think about what is the next step,

94
00:05:54,140 --> 00:05:57,950
because we've managed to make one hurdle. And notice how,

95
00:05:57,980 --> 00:06:01,700
when we're at this position, 3,1, this particular square,

96
00:06:02,120 --> 00:06:05,000
it's almost the same as if we were at 1, 1,

97
00:06:05,120 --> 00:06:10,040
because we have to move one step again, turn left, move, turn right, move,

98
00:06:10,070 --> 00:06:12,080
turn, right, move, turn left.

99
00:06:12,380 --> 00:06:17,380
So it's basically repeating all of these instructions, rather than executing

100
00:06:18,140 --> 00:06:22,490
these instructions six times, because there are six hurdles.

101
00:06:23,120 --> 00:06:25,430
So if I go back and I start,

102
00:06:25,520 --> 00:06:28,910
then you'll see that this code actually will complete the challenge.

103
00:06:31,980 --> 00:06:32,813
Right?

104
00:06:32,820 --> 00:06:35,790
It says I'm done. And we're at the correct position,

105
00:06:35,940 --> 00:06:38,160
but notice how many lines of code we've got.

106
00:06:38,160 --> 00:06:41,340
We've got something like 58 lines of code.

107
00:06:41,400 --> 00:06:44,700
I know I've left some spaces in their so that you can see which parts are

108
00:06:44,700 --> 00:06:49,290
repeating, but still that's a massive amount of code for something very simple.

109
00:06:49,860 --> 00:06:53,040
Now we know that we can use functions to package

110
00:06:53,100 --> 00:06:55,680
a set of instructions together under one name,

111
00:06:56,040 --> 00:06:58,560
just like what we've done here with the turn_right.

112
00:06:58,920 --> 00:07:02,010
We've packaged the three lines of code turn left, turn left, turn

113
00:07:02,010 --> 00:07:05,040
left into a single function called turn_right

114
00:07:05,460 --> 00:07:09,930
so that when we need that functionality, all we need to do is just the call

115
00:07:09,930 --> 00:07:12,360
the function and add the parentheses at the end.

116
00:07:12,960 --> 00:07:17,550
So we can do exactly the same thing with this set of instructions,

117
00:07:17,610 --> 00:07:21,360
which basically gets our robot to perform a single jump.

118
00:07:22,050 --> 00:07:25,260
So let's create a def and let's call this function jump.

119
00:07:25,920 --> 00:07:30,420
And then lets add the parentheses and the colon. And very importantly,

120
00:07:30,450 --> 00:07:34,590
all of these lines of code must be indented. So to indent

121
00:07:34,590 --> 00:07:36,090
a whole block of code together,

122
00:07:36,330 --> 00:07:41,330
you hold down the command key and click the left square bracket or the right

123
00:07:41,460 --> 00:07:45,600
square bracket. On windows it's holding down the control key and again,

124
00:07:45,600 --> 00:07:48,450
the last square bracket or the right square bracket.

125
00:07:49,140 --> 00:07:54,140
So now this basically says that all of these instructions live inside this block

126
00:07:54,660 --> 00:07:58,890
of code called jump. And when we call jump,

127
00:07:59,760 --> 00:08:03,300
then it will carry out all of these lines of code.

128
00:08:04,680 --> 00:08:09,680
So now what we could do to complete the challenge is to simply call this

129
00:08:09,810 --> 00:08:11,610
function jump six times.

130
00:08:12,690 --> 00:08:13,523
Right?

131
00:08:14,740 --> 00:08:19,740
And so now we could again solve this entire problem using just 21 lines of code.

132
00:08:23,050 --> 00:08:28,050
And we managed to complete this challenge using two functions that have repeated

133
00:08:28,720 --> 00:08:32,559
instructions and then calling the jump function six times.

134
00:08:33,130 --> 00:08:37,750
But because we've learned about loops and the range function,

135
00:08:38,110 --> 00:08:41,350
we know that we can actually cut this down even shorter.

136
00:08:41,890 --> 00:08:44,290
So instead of calling jump six times,

137
00:08:44,320 --> 00:08:49,320
we could actually write a for loop that loops through and calls this jump

138
00:08:50,290 --> 00:08:50,860
function

139
00:08:50,860 --> 00:08:55,860
six times. We could say something like for step in,

140
00:08:56,880 --> 00:09:00,090
and remember that these two key words come from the

141
00:09:00,090 --> 00:09:02,730
for...in loop and they always have to stay the same.

142
00:09:03,480 --> 00:09:05,280
And then after the in keyword,

143
00:09:05,400 --> 00:09:09,600
we define the rules for how many times we want our loop to repeat.

144
00:09:10,050 --> 00:09:13,020
In my case, I'm going to use in range function.

145
00:09:13,530 --> 00:09:17,370
And I'm going to say from 0 to 6,

146
00:09:17,700 --> 00:09:22,380
but not including 6. So in this case it will be 0, 1, 2, 

147
00:09:22,410 --> 00:09:23,910
3, 4, 5.

148
00:09:24,270 --> 00:09:27,420
So that actually going to happen six times.

149
00:09:28,020 --> 00:09:32,070
And now I'm going to add the colon. And finally,

150
00:09:32,160 --> 00:09:33,930
inside this for loop,

151
00:09:33,960 --> 00:09:37,650
I'm going to call jump just once. Now,

152
00:09:37,650 --> 00:09:41,790
watch what happens. First it starts off at the for loop,

153
00:09:42,180 --> 00:09:47,180
it calls jump and it goes through this once and then it comes back loops again,

154
00:09:47,880 --> 00:09:51,690
calls it the next time. And then again,

155
00:09:51,840 --> 00:09:55,680
the third time in the loop, fourth time in the loop,

156
00:09:56,280 --> 00:09:59,520
fifth time in the loop and six time in the loop.

157
00:10:01,880 --> 00:10:03,500
How far did you manage to get?

158
00:10:03,950 --> 00:10:08,630
Did you get stuck on this section where we needed to create the for loop?

159
00:10:09,200 --> 00:10:10,610
Well, in that case,

160
00:10:10,640 --> 00:10:14,840
I recommend going back to the lesson where we covered for loops and reviewing

161
00:10:14,840 --> 00:10:19,520
the for loop and the range function in detail before you come back and try to

162
00:10:19,520 --> 00:10:21,980
solve this challenge again. It's really,

163
00:10:21,980 --> 00:10:26,600
really important at this stage that you've understood and you've mastered some

164
00:10:26,600 --> 00:10:31,250
of these ideas because we're going to be using them more and more in the future.

165
00:10:31,910 --> 00:10:35,360
Really make sure that you've understood things before you keep going.

