All language subtitles for 058 The Hurdles Loop Challenge.en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian Download
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

1

All right. So now that you've learned about functions,

2

I have a coding challenge for you. If you head over to this link,

3

which is in the course resources and head over to this challenge

4

which is called Hurdle 1,

5

the idea is that we've got a robot here which needs to do a number of hurdles to

6

jump over each of these barriers, to get to the final goal here.

7

Now you need to use your knowledge of functions

8

as well as some of the things you've learned before, such as the

9

for loop or the range function, to be able to achieve this.

10

Now, remember that you can tell the robot to move by typing, move(),

11

and when that's run, it will move by one space.

12

And you can also use, say,

13

turn_left to get the robot to turn left.

14

And in this case, when I run the code,

15

it's going to move one space and face to the left.

16

In order to complete this entire challenge,

17

you can see there's a number of steps that the robot has to go through before it

18

can get to this final goal. And if you were to write out each of these steps,

19

like I have done here one after the other, it would take many,

20

many tens of lines of code. And that's not what we're about. We're programmers.

21

So we're born to be lazy.

22

Try and see if you can minimize number lines of code while still keeping your

23

code readable and understandable by somebody else and be able to get your robot

24

to complete these instructions and take the robot to the final goalpost.

25

And once you've completed the challenge successfully,

26

you should get a green popup that says you're at the correct X and Y position.

27

So pause the video now,

28

head over to this link and give this challenge a go.

29

Right.

30

All right. So how did you get on with this challenge?

31

How many lines of code did it take you to tell this robot to get to the goal?

32

All right. Let's have a think about how we might tackle this.

33

We know that out of the things that we can tell the robot to do, the most useful

34

things are to move and to turn left.

35

But because the robot doesn't have a builtin turn right function,

36

then we have to define that ourselves.

37

And the reason why we need to do that is because you can see that at several

38

points during the hurdle, we'll have to turn right. For example,

39

when the robot is here at 2, 2, it will have to turn right,

40

and then move and then turn right again and then move.

41

So let's create a new function called turn_right.

42

So we start out with the def key keyword and then give our function a name which

43

is going to be turn_right.

44

And then we add the parentheses with nothing inside

45

because we're not passing any inputs to this function.

46

We're just going to use this function as a way of defining turn left three

47

times. So when you turn left three times, of course you turn right.

48

And now we have the ability to turn right.

49

So notice how this is the code block for our turn right function

50

and it ends at the last line, which is indented, which is line 4.

51

So every subsequent line

52

which starts at the very beginning of the code file next to

53

the left margin is outside this code block and it will act independently.

54

So in fact,

55

the first line of code that actually runs in our file is going to be this line

56

6. And you can see that when I press run,

57

you can see that's the first thing that gets highlighted.

58

And if I go through this step wise,

59

you can see the first line is going to be line 6 and it tells the computer to

60

go and find this function called turn_right. So if I skipped to the next step,

61

it's found that, and it's going to start going through lines 2, 3,

62

and 4,

63

turning our robot three times until it has turned right. Now that we have the

64

ability to turn right,

65

let's have a think about how we might be able to get our robot to make one jump.

66

Let's see. The first thing we'd probably want our robot to do is to move forwards

67

by one step, which should take it here.

68

And then the next thing we want it to do is the turn left so that it faces the

69

top.

70

Right?

71

And once it's in this position,

72

then we want to get it to move forward one step,

73

and then we'll probably want it to turn right. Now,

74

notice how I'm actually testing my code at pretty much every other line that I

75

write.

76

And this means that you don't end up in a situation where you get to the end and

77

you've written lots and lots of lines of code, and it doesn't work.

78

In which case you have to comb through all of the code you've written and find

79

out where the problem is. So in this case,

80

we've only got four lines and we've been testing it step wise.

81

So we can see that we're on track and our robot is now turned to the right,

82

ready to move one more step.

83

And then once it gets to here,

84

we have to turn right again and move one more step.

85

Right?

86

And now it should end up here, but let's just check it out.

87

So our robot turns left turns right, turns right again.

88

And it ends up here facing the bottom.

89

So the final thing we need to do is to get it, to turn left once more,

90

so that it will face the forward direction.

91

Right?

92

So once you've confirmed that your code is working,

93

then you should really think about what is the next step,

94

because we've managed to make one hurdle. And notice how,

95

when we're at this position, 3,1, this particular square,

96

it's almost the same as if we were at 1, 1,

97

because we have to move one step again, turn left, move, turn right, move,

98

turn, right, move, turn left.

99

So it's basically repeating all of these instructions, rather than executing

100

these instructions six times, because there are six hurdles.

101

So if I go back and I start,

102

then you'll see that this code actually will complete the challenge.

103

Right?

104

It says I'm done. And we're at the correct position,

105

but notice how many lines of code we've got.

106

We've got something like 58 lines of code.

107

I know I've left some spaces in their so that you can see which parts are

108

repeating, but still that's a massive amount of code for something very simple.

109

Now we know that we can use functions to package

110

a set of instructions together under one name,

111

just like what we've done here with the turn_right.

112

We've packaged the three lines of code turn left, turn left, turn

113

left into a single function called turn_right

114

so that when we need that functionality, all we need to do is just the call

115

the function and add the parentheses at the end.

116

So we can do exactly the same thing with this set of instructions,

117

which basically gets our robot to perform a single jump.

118

So let's create a def and let's call this function jump.

119

And then lets add the parentheses and the colon. And very importantly,

120

all of these lines of code must be indented. So to indent

121

a whole block of code together,

122

you hold down the command key and click the left square bracket or the right

123

square bracket. On windows it's holding down the control key and again,

124

the last square bracket or the right square bracket.

125

So now this basically says that all of these instructions live inside this block

126

of code called jump. And when we call jump,

127

then it will carry out all of these lines of code.

128

So now what we could do to complete the challenge is to simply call this

129

function jump six times.

130

Right?

131

And so now we could again solve this entire problem using just 21 lines of code.

132

And we managed to complete this challenge using two functions that have repeated

133

instructions and then calling the jump function six times.

134

But because we've learned about loops and the range function,

135

we know that we can actually cut this down even shorter.

136

So instead of calling jump six times,

137

we could actually write a for loop that loops through and calls this jump

138

function

139

six times. We could say something like for step in,

140

and remember that these two key words come from the

141

for...in loop and they always have to stay the same.

142

And then after the in keyword,

143

we define the rules for how many times we want our loop to repeat.

144

In my case, I'm going to use in range function.

145

And I'm going to say from 0 to 6,

146

but not including 6. So in this case it will be 0, 1, 2,

147

3, 4, 5.

148

So that actually going to happen six times.

149

And now I'm going to add the colon. And finally,

150

inside this for loop,

151

I'm going to call jump just once. Now,

152

watch what happens. First it starts off at the for loop,

153

it calls jump and it goes through this once and then it comes back loops again,

154

calls it the next time. And then again,

155

the third time in the loop, fourth time in the loop,

156

fifth time in the loop and six time in the loop.

157

How far did you manage to get?

158

Did you get stuck on this section where we needed to create the for loop?

159

Well, in that case,

160

I recommend going back to the lesson where we covered for loops and reviewing

161

the for loop and the range function in detail before you come back and try to

162

solve this challenge again. It's really,

163

really important at this stage that you've understood and you've mastered some

164

of these ideas because we're going to be using them more and more in the future.

165

Really make sure that you've understood things before you keep going.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.