1
00:00:00,570 --> 00:00:04,740
Head over to day 5.1, the average height exercise.

2
00:00:06,000 --> 00:00:09,300
Now the instructions are that you're going to write a program

3
00:00:09,390 --> 00:00:13,470
that's going to calculate the average student height from a list of heights.

4
00:00:13,830 --> 00:00:14,880
So for example,

5
00:00:14,940 --> 00:00:18,420
let's say we had a list of student heights that looks something like this,

6
00:00:18,780 --> 00:00:21,150
then you're going to be using a for loop,

7
00:00:21,420 --> 00:00:24,030
hopefully, to figure out the average.

8
00:00:24,690 --> 00:00:29,160
And the average can be calculated by adding up everybody's height and then dividing

9
00:00:29,160 --> 00:00:31,770
it by the number of students inside the list.

10
00:00:32,370 --> 00:00:35,730
And then I want you to round it to the nearest whole number.

11
00:00:37,620 --> 00:00:38,010
Now,

12
00:00:38,010 --> 00:00:40,350
notice how we've got some starting code here.

13
00:00:40,590 --> 00:00:45,590
And this is just for us to be able to input a list of student heights for testing

14
00:00:46,200 --> 00:00:50,160
and see if your code actually works. So if you go ahead and run the code,

15
00:00:50,430 --> 00:00:54,180
then you'll notice that it asks you for a list of student heights.

16
00:00:54,660 --> 00:00:57,410
And the input is going to look something like this.

17
00:00:57,420 --> 00:01:00,810
They're just going to be a bunch of numbers separated by a space.

18
00:01:01,140 --> 00:01:04,560
So if I go ahead and paste that in there, then I hit enter,

19
00:01:04,890 --> 00:01:09,890
then notice how this line on line five is going to print it out just as a normal

20
00:01:10,380 --> 00:01:13,290
list full of numbers. Now,

21
00:01:13,320 --> 00:01:17,040
Python has some really useful functions as we've seen.

22
00:01:17,100 --> 00:01:22,100
We've seen that we could use the len function to calculate the size of a

23
00:01:22,770 --> 00:01:23,760
particular list.

24
00:01:24,180 --> 00:01:28,440
So if I go ahead and print my length of student heights,

25
00:01:29,490 --> 00:01:32,160
and then I paste my student heights in here,

26
00:01:32,490 --> 00:01:37,080
then you can see that I get five printed by calculating the number of items in

27
00:01:37,080 --> 00:01:41,310
here. Now, Python also has something called sum,

28
00:01:41,820 --> 00:01:46,820
and this gives us the total of all the items from a list.

29
00:01:48,330 --> 00:01:51,840
So in this case, all of these added up would be 857.

30
00:01:52,590 --> 00:01:53,760
Now your job though,

31
00:01:53,790 --> 00:01:58,140
is to figure out how these functions actually work under the hood.

32
00:01:58,680 --> 00:02:01,020
In order to fully complete this challenge,

33
00:02:01,440 --> 00:02:06,180
you can't use the len function or the sum function.

34
00:02:06,510 --> 00:02:11,190
Instead, you have to figure out another way through the use of for loops

35
00:02:11,580 --> 00:02:15,720
where you can add each of the items inside the list together,

36
00:02:16,320 --> 00:02:21,320
and you can use a for loop to figure out how many items there are in this list

37
00:02:22,470 --> 00:02:27,470
and use that to calculate the average. So you can't use either of these

38
00:02:27,510 --> 00:02:28,343
functions

39
00:02:28,560 --> 00:02:32,130
and instead, you have to rely on what you learned in the last lesson to complete

40
00:02:32,130 --> 00:02:36,360
this challenge. So have a think about it, and once you are ready, give that a go.

41
00:02:42,980 --> 00:02:43,250
Now,

42
00:02:43,250 --> 00:02:46,460
we know that we can't use either of these functions,

43
00:02:46,520 --> 00:02:50,330
the sum function or the length function, because if we could,

44
00:02:50,360 --> 00:02:54,620
then this challenge would be super easy and we would be able to calculate the

45
00:02:54,620 --> 00:02:58,910
average by getting the total height, by getting the

46
00:02:59,890 --> 00:03:03,100
sum of all of the items inside student heights.

47
00:03:03,610 --> 00:03:06,610
And then we can calculate the number of students

48
00:03:09,040 --> 00:03:10,930
by using the length function

49
00:03:10,990 --> 00:03:14,620
to calculate the number of items inside the list.

50
00:03:15,070 --> 00:03:16,810
And then we would get the average height

51
00:03:19,000 --> 00:03:22,900
by dividing the total height by the numbers of students.

52
00:03:23,230 --> 00:03:28,230
And then we would be able to round this value off and print it out.

53
00:03:30,520 --> 00:03:34,390
But this, as you noticed, doesn't use a single for loop.

54
00:03:34,750 --> 00:03:37,990
And the last thing we learned about was a for loop.

55
00:03:38,080 --> 00:03:43,080
So this is definitely not the right way to solve the challenge. It works,

56
00:03:43,360 --> 00:03:48,360
but what I wanted you to do is to figure out how the sum and the len functions

57
00:03:48,940 --> 00:03:52,960
work under the hood. How is it actually implemented?

58
00:03:53,560 --> 00:03:56,530
And to do that, we have to use a for loop.

59
00:03:56,920 --> 00:04:00,340
So let's have a think about how we would first tackle this one,

60
00:04:00,940 --> 00:04:05,940
where we get the total of all the items inside student heights.

61
00:04:06,970 --> 00:04:11,970
So we basically want to add each of them together until we get the total.

62
00:04:12,730 --> 00:04:16,840
Now, we can do this by simply using a for loop as you've learned in the last

63
00:04:16,840 --> 00:04:20,650
lesson. So if we start off total_height as zero,

64
00:04:21,160 --> 00:04:26,160
and then we write a for loop and we say for height in student_heights,

65
00:04:29,350 --> 00:04:30,850
and then we add a colon,

66
00:04:31,540 --> 00:04:35,350
then now we're going to loop through each of the heights inside this list

67
00:04:35,620 --> 00:04:37,930
and we're going to add it to our total height.

68
00:04:38,200 --> 00:04:43,200
So we can say total_height equals previous value of total_height plus height.

69
00:04:44,170 --> 00:04:45,730
Or as you might remember,

70
00:04:45,760 --> 00:04:50,740
the shortened version of this is just total height plus equals each of the

71
00:04:50,740 --> 00:04:51,573
heights.

72
00:04:51,790 --> 00:04:56,790
So now we can go ahead and check whether if this works by printing out the final

73
00:04:57,970 --> 00:05:02,410
value of total_height. And notice how I'm doing it outside the loop

74
00:05:02,530 --> 00:05:05,500
by making sure that it's not indented like this.

75
00:05:05,980 --> 00:05:10,980
So let's run our code and I'm going to paste in those numbers as the demo

76
00:05:12,310 --> 00:05:15,340
numbers. And then we get the value

77
00:05:15,340 --> 00:05:20,230
total_height is 857, which if you take a calculator,

78
00:05:20,560 --> 00:05:23,170
you'll be able to verify its exactly right.

79
00:05:23,980 --> 00:05:27,940
So now we've basically replicated how the sum function works.

80
00:05:28,450 --> 00:05:31,900
And now that we can try and replicate how the len function works.

81
00:05:32,380 --> 00:05:32,920
We know that

82
00:05:32,920 --> 00:05:37,630
basically the len function figures out how many items are in a particular list.

83
00:05:38,200 --> 00:05:42,490
How can we do this using a for loop instead? Well,

84
00:05:42,490 --> 00:05:46,360
what if we do something similar to what we did above where we start off the

85
00:05:46,360 --> 00:05:48,340
numbers of students as zero,

86
00:05:48,820 --> 00:05:53,820
and then we write a for loop for students in student heights, and inside our for

87
00:05:55,450 --> 00:05:55,990
loop

88
00:05:55,990 --> 00:06:00,990
all that we do is that we add one to our numbers of students each time

89
00:06:02,600 --> 00:06:06,440
our for loop runs. And if you remember from the last lesson,

90
00:06:06,440 --> 00:06:11,440
I said that the for loop will run for as many times as there are items inside

91
00:06:12,320 --> 00:06:15,440
the list. If there are five items inside the list,

92
00:06:15,740 --> 00:06:18,530
then the for loop is going to run five times,

93
00:06:18,770 --> 00:06:22,220
which means this line of code is going to be called five times,

94
00:06:22,460 --> 00:06:26,450
which means we add five to our numbers of students

95
00:06:26,750 --> 00:06:30,140
and we end up with the total_length of the list.

96
00:06:31,070 --> 00:06:36,070
So now let's go ahead and print this number of students out and run our code.

97
00:06:38,300 --> 00:06:40,310
And I'm going to paste in the same

98
00:06:40,310 --> 00:06:43,580
five numbers in the example input.

99
00:06:44,180 --> 00:06:49,180
And you can see that the first value that comes out is my total height,

100
00:06:49,760 --> 00:06:54,050
the second value is the length of the list, the number of items in the list

101
00:06:54,080 --> 00:06:58,550
which is five, we can verify that. And the final one is the average

102
00:06:58,610 --> 00:07:00,740
which matches up with the example output.

103
00:07:01,310 --> 00:07:05,720
So now let's get rid of our print statements by commenting them out.

104
00:07:08,210 --> 00:07:08,510
Now,

105
00:07:08,510 --> 00:07:12,290
one of the things I want to point out to you that you might've noticed is that

106
00:07:12,620 --> 00:07:17,150
in the first for loop, even though we're looping through the same list,

107
00:07:17,600 --> 00:07:20,150
I've called each of the items height.

108
00:07:20,600 --> 00:07:24,140
And in the second for-loop I've called them students. Now,

109
00:07:24,140 --> 00:07:29,030
whatever we name each of the items inside the list, it's totally up to you.

110
00:07:29,030 --> 00:07:32,240
If you could call it h, you could call it x,

111
00:07:32,270 --> 00:07:34,700
you could call it basically anything you want

112
00:07:35,030 --> 00:07:38,780
as long as you are consistent with using it inside the for loop.

113
00:07:39,260 --> 00:07:42,200
So if you're calling each of the items inside the list

114
00:07:42,320 --> 00:07:45,530
h, then when you're using it inside the for loop,

115
00:07:45,530 --> 00:07:47,660
when you're modifying it or doing something with it,

116
00:07:47,960 --> 00:07:50,870
then you are going to refer to it by the same name. Now,

117
00:07:51,020 --> 00:07:54,380
usually it's a good idea to give it the name

118
00:07:54,620 --> 00:07:59,180
which is the singular form of each of the items in the list.

119
00:07:59,540 --> 00:08:04,520
For example, previously, we had a list of fruits. So each item was called fruit,

120
00:08:04,580 --> 00:08:08,480
the singular form. And in this case, we have a bunch of heights,

121
00:08:08,750 --> 00:08:12,560
so each of them I've called height. In the second for loop

122
00:08:12,740 --> 00:08:17,030
I've decided to call each of the items a student. Now, in this case,

123
00:08:17,360 --> 00:08:21,380
even though you have two for loops, you can call them the same thing.

124
00:08:21,950 --> 00:08:26,360
But sometimes I find it a little bit confusing to have the same name in two

125
00:08:26,360 --> 00:08:29,780
for loops. I prefer it this way, but it's totally up to you.

126
00:08:30,050 --> 00:08:34,850
Just follow the good practice of using a singular form of whatever is contained

127
00:08:34,850 --> 00:08:37,550
in a list when you write a for loop like this,

128
00:08:37,730 --> 00:08:39,320
and you'll have less trouble down the line.

