Average height in python with source code

Average height in python with source code

Introduction

Average height is a programming exercise in python. This program finds the average height from various numbers of heights entered.   

In this tutorial, we are creating a program that prints the average height. It is a simple python program for brainstorming. We are using for loop in this exercise. The program will ask the user to enter a list of heights in cm more than five and evaluate the height. The code is simple and easy to understand.

Input method is used to make user enter their heights. After that, the string is converted into an integer. The user input is in string format before it needs to be converted into an integer. Now the for loop is used and the height is calculated, as dividing the total height by total no of person. See the code and output below.

student_heights = input("Input a list of student heights ").split()
for n in range(0, len(student_heights)):
  student_heights[n] = int(student_heights[n])

print(student_heights)
total_height = 0
for height in student_heights:
 total_height += height
print(f"Total height: {total_height}")

number_of_students = 0
for student in student_heights:
 number_of_students += 1
print(f"Total students: {number_of_students}")

average_height = round(total_height / number_of_students)
print(f"The average height is {average_height}")

output

average height

How to use this average height project?

  • Copy and paste the source code in your editor.(pycharm, anaconda)
  • Execute the source code.
  • Get the height.
  • Enjoy and share!