All language subtitles for 052 [Interactive Coding Exercise] Adding Even Numbers.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
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
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

Click on 5.3 adding evens.

2

The exercise is kind of similar to what Gauss had to do that I showed you in the

3

last lesson, but in your case,

4

I want you to calculate the sum of all the even numbers from 1 to 100,

5

including 1 and 100. So for example,

6

it might be two plus four plus six plus eight,

7

and so on and so forth until you get to a hundred.

8

It's a little bit more complicated than what the ten-year-old had to do.

9

And the important thing to remember is that there should only be one print

10

statement in your output.

11

It should just print the final total and not every step of the calculation.

12

So have a think about how you might solve this challenge,

13

pause the video and try to give this a go.

14

All right.

15

So there's actually quite a few ways that you could have solved this challenge.

16

Now,

17

one of the ways would have been using what we learned from the last lesson and the

18

ability to create a range function using a different step size.

19

I'll show you what I mean.

20

So let's create a full loop where we say for every number in the range between

21

1 to 100, so that's 101,

22

then we're going to do something with each number, right? Namely,

23

we're probably going to create a variable called total

24

and then we're going to add each of the even numbers together and accumulate it

25

inside this variable.

26

Now remember that I mentioned that the range function actually has a third thing

27

that you can add to it, not just the start and the end,

28

but also the step size. so we can change the step size to two,

29

and then now what it's going to do is it's going to go through 1 to 100 and

30

it's going to increase the step size by two.

31

So that means if we print this range as it is right now,

32

it's going to give us one plus two which is three, plus two

33

which is five and so on and so forth.

34

But if we change the range to start off from two

35

which is the first number that we have to start adding in our sequence, and then

36

increase it by two every single time,

37

then we end up with a range that includes all of the even numbers up to a

38

hundred. And I'll show you if I run this code right now.

39

So the next part is simple. All we have to do is to add the number to

40

the current total. So total += number.

41

And then finally outside the for loop,

42

you can go ahead and print that total. Now,

43

when I go ahead and run this code now, it's going to give me just the total.

44

Now remember that the indentation really matters because if I have it like this,

45

then it's going to give me the total for every step of the calculation,

46

because it's now included in the for loop and will be executed for as many times

47

as the for loop runs. But if I remove the indentation, then the

48

for loop is just this and this is outside of it

49

so it's only going to give it to me once everything has been done

50

and I've got the final total. And it's the correct solution.

51

But you might have also done it in a different way.

52

You might have said for a number in range,

53

1 to 101,

54

and you had some sort of another total, let's call it total2

55

and started at zero,

56

and then we say that if the number % 2 is equal to 0,

57

so if it's even essentially,

58

then we're going to add this number to the total2.

59

Finally, outside of the if and outside of the for,

60

we're going to print the final total. Now,

61

if I just run the code, then you can see

62

I get exactly the same number as before.

63

So if you chose to solve this challenge in either of these ways,

64

then you've succeeded. They're both right.

65

And there's probably even other ways that you could solve this as well.

66

But these two, I suspect will be the most popular approaches.

67

Now in the next lesson, I've got another coding exercise for you

68

and it's probably one of the top interview questions that programmers get asked

69

when they're being interviewed for a job. So head over there and give it a go.

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