In: Computer Science
Your task is to calculate the average height of students in the class. You will gather each of the student’s height and put it in a list. then you will throw out the shortest and tallest height to take care of any statistical anomalies. Then you will add the heights and divide by the number of student’s in the class.
Heights of Students in inches
Max=77
Josefine=67
Hannah=66
Mia=65
Ben=74
Kevin=70
Marie=64
Aidan=70
Steps On Python:
Declare a list to contain the heights in inches
Ask the user to input values
Add each value to the list
Use appropriate list function to find smallest object
Use appropriate list function to remove that smallest object
Use appropriate list function to find largest object
Use appropriate list function to remove that largest object
Use appropriate list function to add objects in the list,
Then Use appropriate list function to divide that amount by the number of objects in the list
Then print the objects and the average height of students in the class.
********************************************************solution*******************************************************
*Codes , code screenshot, code output screenshot are attached below
----------------------------------------------------CODE-----------------------------------------------------
#list declaration
heights
heights=[]
#converting num into int and input
number of students
num=int(input("Enter the number of students:"))
#input heights from
user
print("Enter the heights of students in inches:")
for i in range(num):
x=int(input("->"))
heights.append(x)
#Minimum found using "min"
function
Min=min(heights)
#removing Minimum height from the
list
heights.remove(Min)
#Maximum found using "max"
function
Max=max(heights)
#removing Maximum height from the
list
heights.remove(Max)
#total sum of
heights
sum=0
for height in heights:
sum=sum+height
#average (max and min excluded so
thats why (num-2))
average=sum/(num-2)
#printing the
objects
print("\nThe remaining heights of the students are :")
for height in heights:
print(height,end=" ")
#printing average height
#round function is used to round floats number upto 2
decimal
print("\nThe Average height of the students is
"+str(round(average,2)))
----------------------------------------------------CODE SCREENSHOT-----------------------------------------------------
----------------------------------------------------CODE OUTPUT SCREENSHOT-----------------------------------------------------