Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
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.