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
Head over to day 5.1, the average height exercise.
2
Now the instructions are that you're going to write a program
3
that's going to calculate the average student height from a list of heights.
4
So for example,
5
let's say we had a list of student heights that looks something like this,
6
then you're going to be using a for loop,
7
hopefully, to figure out the average.
8
And the average can be calculated by adding up everybody's height and then dividing
9
it by the number of students inside the list.
10
And then I want you to round it to the nearest whole number.
11
Now,
12
notice how we've got some starting code here.
13
And this is just for us to be able to input a list of student heights for testing
14
and see if your code actually works. So if you go ahead and run the code,
15
then you'll notice that it asks you for a list of student heights.
16
And the input is going to look something like this.
17
They're just going to be a bunch of numbers separated by a space.
18
So if I go ahead and paste that in there, then I hit enter,
19
then notice how this line on line five is going to print it out just as a normal
20
list full of numbers. Now,
21
Python has some really useful functions as we've seen.
22
We've seen that we could use the len function to calculate the size of a
23
particular list.
24
So if I go ahead and print my length of student heights,
25
and then I paste my student heights in here,
26
then you can see that I get five printed by calculating the number of items in
27
here. Now, Python also has something called sum,
28
and this gives us the total of all the items from a list.
29
So in this case, all of these added up would be 857.
30
Now your job though,
31
is to figure out how these functions actually work under the hood.
32
In order to fully complete this challenge,
33
you can't use the len function or the sum function.
34
Instead, you have to figure out another way through the use of for loops
35
where you can add each of the items inside the list together,
36
and you can use a for loop to figure out how many items there are in this list
37
and use that to calculate the average. So you can't use either of these
38
functions
39
and instead, you have to rely on what you learned in the last lesson to complete
40
this challenge. So have a think about it, and once you are ready, give that a go.
41
Now,
42
we know that we can't use either of these functions,
43
the sum function or the length function, because if we could,
44
then this challenge would be super easy and we would be able to calculate the
45
average by getting the total height, by getting the
46
sum of all of the items inside student heights.
47
And then we can calculate the number of students
48
by using the length function
49
to calculate the number of items inside the list.
50
And then we would get the average height
51
by dividing the total height by the numbers of students.
52
And then we would be able to round this value off and print it out.
53
But this, as you noticed, doesn't use a single for loop.
54
And the last thing we learned about was a for loop.
55
So this is definitely not the right way to solve the challenge. It works,
56
but what I wanted you to do is to figure out how the sum and the len functions
57
work under the hood. How is it actually implemented?
58
And to do that, we have to use a for loop.
59
So let's have a think about how we would first tackle this one,
60
where we get the total of all the items inside student heights.
61
So we basically want to add each of them together until we get the total.
62
Now, we can do this by simply using a for loop as you've learned in the last
63
lesson. So if we start off total_height as zero,
64
and then we write a for loop and we say for height in student_heights,
65
and then we add a colon,
66
then now we're going to loop through each of the heights inside this list
67
and we're going to add it to our total height.
68
So we can say total_height equals previous value of total_height plus height.
69
Or as you might remember,
70
the shortened version of this is just total height plus equals each of the
71
heights.
72
So now we can go ahead and check whether if this works by printing out the final
73
value of total_height. And notice how I'm doing it outside the loop
74
by making sure that it's not indented like this.
75
So let's run our code and I'm going to paste in those numbers as the demo
76
numbers. And then we get the value
77
total_height is 857, which if you take a calculator,
78
you'll be able to verify its exactly right.
79
So now we've basically replicated how the sum function works.
80
And now that we can try and replicate how the len function works.
81
We know that
82
basically the len function figures out how many items are in a particular list.
83
How can we do this using a for loop instead? Well,
84
what if we do something similar to what we did above where we start off the
85
numbers of students as zero,
86
and then we write a for loop for students in student heights, and inside our for
87
loop
88
all that we do is that we add one to our numbers of students each time
89
our for loop runs. And if you remember from the last lesson,
90
I said that the for loop will run for as many times as there are items inside
91
the list. If there are five items inside the list,
92
then the for loop is going to run five times,
93
which means this line of code is going to be called five times,
94
which means we add five to our numbers of students
95
and we end up with the total_length of the list.
96
So now let's go ahead and print this number of students out and run our code.
97
And I'm going to paste in the same
98
five numbers in the example input.
99
And you can see that the first value that comes out is my total height,
100
the second value is the length of the list, the number of items in the list
101
which is five, we can verify that. And the final one is the average
102
which matches up with the example output.
103
So now let's get rid of our print statements by commenting them out.
104
Now,
105
one of the things I want to point out to you that you might've noticed is that
106
in the first for loop, even though we're looping through the same list,
107
I've called each of the items height.
108
And in the second for-loop I've called them students. Now,
109
whatever we name each of the items inside the list, it's totally up to you.
110
If you could call it h, you could call it x,
111
you could call it basically anything you want
112
as long as you are consistent with using it inside the for loop.
113
So if you're calling each of the items inside the list
114
h, then when you're using it inside the for loop,
115
when you're modifying it or doing something with it,
116
then you are going to refer to it by the same name. Now,
117
usually it's a good idea to give it the name
118
which is the singular form of each of the items in the list.
119
For example, previously, we had a list of fruits. So each item was called fruit,
120
the singular form. And in this case, we have a bunch of heights,
121
so each of them I've called height. In the second for loop
122
I've decided to call each of the items a student. Now, in this case,
123
even though you have two for loops, you can call them the same thing.
124
But sometimes I find it a little bit confusing to have the same name in two
125
for loops. I prefer it this way, but it's totally up to you.
126
Just follow the good practice of using a singular form of whatever is contained
127
in a list when you write a for loop like this,
128
and you'll have less trouble down the line.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.