1
00:00:00,570 --> 00:00:01,620
In this exercise,

2
00:00:01,620 --> 00:00:03,180
you're going to write a program

3
00:00:03,180 --> 00:00:07,650
that works out the average height of a class of students.

4
00:00:07,650 --> 00:00:08,730
In the Input pane,

5
00:00:08,730 --> 00:00:12,150
we have all of the students' heights in centimeters

6
00:00:12,150 --> 00:00:17,150
and through lines two to four, we convert that into a list.

7
00:00:17,970 --> 00:00:22,970
And you can work with it by using the list student_heights.

8
00:00:25,080 --> 00:00:29,850
The only rule is you have to write at least one for loop,

9
00:00:29,850 --> 00:00:32,009
that's the whole point of the exercise.

10
00:00:32,009 --> 00:00:35,430
There are simplified methods from Python,

11
00:00:35,430 --> 00:00:38,370
such as the len() method and the sum() method,

12
00:00:38,370 --> 00:00:41,880
that will make your life a lot easier working with lists,

13
00:00:41,880 --> 00:00:44,400
but the goal here is to ignore those,

14
00:00:44,400 --> 00:00:46,170
pretend they don't exist.

15
00:00:46,170 --> 00:00:49,350
Instead, you are going to write the len functionality

16
00:00:49,350 --> 00:00:53,640
and the sum functionality yourself using a for loop.

17
00:00:53,640 --> 00:00:56,580
And in this process you're going to practice the skill

18
00:00:56,580 --> 00:00:58,260
of writing for loops.

19
00:00:58,260 --> 00:01:00,540
Have a think about how you might achieve this.

20
00:01:00,540 --> 00:01:02,610
Take a look at the Example Input

21
00:01:02,610 --> 00:01:05,190
and the expected Example Output,

22
00:01:05,190 --> 00:01:07,140
I want to see the total height printed,

23
00:01:07,140 --> 00:01:08,610
the number of students printed,

24
00:01:08,610 --> 00:01:10,410
and the average height printed

25
00:01:10,410 --> 00:01:13,590
in the exact format that you see in the Example Outputs.

26
00:01:13,590 --> 00:01:14,763
So give it a go now.

27
00:01:20,490 --> 00:01:22,800
Alright, so the only rule in this exercise

28
00:01:22,800 --> 00:01:25,530
is you have to write some for loops

29
00:01:25,530 --> 00:01:28,410
and not use the len() or the sum() methods.

30
00:01:28,410 --> 00:01:32,400
We all know how the methods from Python is lifesaving

31
00:01:32,400 --> 00:01:34,860
and it can be so much easier,

32
00:01:34,860 --> 00:01:37,830
but in this exercise we're practicing our for loops

33
00:01:37,830 --> 00:01:40,383
so we're going to pretend they don't exist.

34
00:01:41,880 --> 00:01:44,310
The first step, is I'm going to create a variable

35
00:01:44,310 --> 00:01:47,730
called total_height and set it equal to 0,

36
00:01:47,730 --> 00:01:50,460
and I'm going to accumulate the heights that I get

37
00:01:50,460 --> 00:01:53,010
in the list to this variable.

38
00:01:53,010 --> 00:01:54,870
I create a for loop saying,

39
00:01:54,870 --> 00:01:59,010
for each height in my list, student_heights,

40
00:01:59,010 --> 00:02:03,210
I'm going to add the height that I get in the list

41
00:02:03,210 --> 00:02:07,140
to the total height, and by using the plus equals symbol (+=),

42
00:02:07,140 --> 00:02:10,830
I can accumulate the value into that variable.

43
00:02:10,830 --> 00:02:13,920
And then finally, I can print out total_height equals

44
00:02:13,920 --> 00:02:17,610
whatever it is at the end, once a for loop has run through

45
00:02:17,610 --> 00:02:21,480
all of the elements in the list of student_heights.

46
00:02:21,480 --> 00:02:24,600
And then finally I can print out total_height equals,

47
00:02:24,600 --> 00:02:27,908
whatever it is at the end, once a for loop has run through

48
00:02:27,908 --> 00:02:32,100
all of the elements in the list of student heights.

49
00:02:32,100 --> 00:02:36,030
Next, I do the same thing using the number_of_students.

50
00:02:36,030 --> 00:02:40,770
So I start that out at 0 and I add 1 for each student

51
00:02:40,770 --> 00:02:42,780
in our list of student_heights.

52
00:02:42,780 --> 00:02:44,670
Now this of course replicates

53
00:02:44,670 --> 00:02:47,460
the Python functionality of len,

54
00:02:47,460 --> 00:02:50,520
but it's really helpful to at least be able

55
00:02:50,520 --> 00:02:52,080
to break down these functions

56
00:02:52,080 --> 00:02:53,880
and write them yourself sometimes,

57
00:02:53,880 --> 00:02:55,200
if not just to be able to prove

58
00:02:55,200 --> 00:02:57,000
to yourself that you can do it.

59
00:02:57,000 --> 00:02:59,580
And finally, once I have accumulated

60
00:02:59,580 --> 00:03:02,370
all of the count of students in my list,

61
00:03:02,370 --> 00:03:04,810
I can print it out, number of students equals

62
00:03:04,810 --> 00:03:08,130
and then insert it in using my f-string.

63
00:03:08,130 --> 00:03:09,900
Finally, we work out the average

64
00:03:09,900 --> 00:03:14,340
by dividing the total_height by the total number_of_students

65
00:03:14,340 --> 00:03:17,880
and then we can round() that number to get a whole number

66
00:03:17,880 --> 00:03:19,260
and then we can print it out,

67
00:03:19,260 --> 00:03:23,100
average height equals whatever it is we calculated

68
00:03:23,100 --> 00:03:25,440
and no matter what different inputs you put in,

69
00:03:25,440 --> 00:03:29,340
as long as they are different numbers separated by a space,

70
00:03:29,340 --> 00:03:32,070
this code should work to calculate the average

71
00:03:32,070 --> 00:03:36,450
without using any of the simplifier methods like len() or sum().

72
00:03:36,450 --> 00:03:38,250
So well done if you managed to do it.

73
00:03:38,250 --> 00:03:40,110
If you used len() or sum(),

74
00:03:40,110 --> 00:03:43,230
then it's time to go back and change your code

75
00:03:43,230 --> 00:03:45,813
and make sure you are practicing using the loops.

