In this exercise,
you're going to write a program
that works out the average height of a class of students.
In the Input pane,
we have all of the students' heights in centimeters
and through lines two to four, we convert that into a list.
And you can work with it by using the list student_heights.
The only rule is you have to write at least one for loop,
that's the whole point of the exercise.
There are simplified methods from Python,
such as the len() method and the sum() method,
that will make your life a lot easier working with lists,
but the goal here is to ignore those,
pretend they don't exist.
Instead, you are going to write the len functionality
and the sum functionality yourself using a for loop.
And in this process you're going to practice the skill
of writing for loops.
Have a think about how you might achieve this.
Take a look at the Example Input
and the expected Example Output,
I want to see the total height printed,
the number of students printed,
and the average height printed
in the exact format that you see in the Example Outputs.
So give it a go now.
Alright, so the only rule in this exercise
is you have to write some for loops
and not use the len() or the sum() methods.
We all know how the methods from Python is lifesaving
and it can be so much easier,
but in this exercise we're practicing our for loops
so we're going to pretend they don't exist.
The first step, is I'm going to create a variable
called total_height and set it equal to 0,
and I'm going to accumulate the heights that I get
in the list to this variable.
I create a for loop saying,
for each height in my list, student_heights,
I'm going to add the height that I get in the list
to the total height, and by using the plus equals symbol (+=),
I can accumulate the value into that variable.
And then finally, I can print out total_height equals
whatever it is at the end, once a for loop has run through
all of the elements in the list of student_heights.
And then finally I can print out total_height equals,
whatever it is at the end, once a for loop has run through
all of the elements in the list of student heights.
Next, I do the same thing using the number_of_students.
So I start that out at 0 and I add 1 for each student
in our list of student_heights.
Now this of course replicates
the Python functionality of len,
but it's really helpful to at least be able
to break down these functions
and write them yourself sometimes,
if not just to be able to prove
to yourself that you can do it.
And finally, once I have accumulated
all of the count of students in my list,
I can print it out, number of students equals
and then insert it in using my f-string.
Finally, we work out the average
by dividing the total_height by the total number_of_students
and then we can round() that number to get a whole number
and then we can print it out,
average height equals whatever it is we calculated
and no matter what different inputs you put in,
as long as they are different numbers separated by a space,
this code should work to calculate the average
without using any of the simplifier methods like len() or sum().
So well done if you managed to do it.
If you used len() or sum(),
then it's time to go back and change your code
and make sure you are practicing using the loops.