2023. 2. 27. 15:42ㆍ개인적인 공부/Python
Instructions
You are going to write a program that calculates the average student height from a List of heights.
e.g. student_heights = [180, 124, 165, 173, 189, 169, 146]
The average height can be calculated by adding all the heights together and dividing by the total number of heights.
e.g.
180 + 124 + 165 + 173 + 189 + 169 + 146 = 1146
There are a total of 7 heights in student_heights
1146 ÷ 7 = 163.71428571428572
Average height rounded to the nearest whole number = 164
Important You should not use the sum() or len() functions in your answer. You should try to replicate their functionality using what you have learnt about for loops.
Example Input
156 178 165 171 187
In this case, student_heights would be a list that looks like: [156, 178, 165, 171, 187]
Example Output
171
e.g. When you hit run, this is what should happen:
### for 문을 통한 평균 구하기
## len(), sum() 을 사용하지 말것
# 🚨 Don't change the code 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])
# 🚨 Don't change the code above 👆
#Write your code below this row 👇
len() 과 sum() 을 쓰면 쉬운데 반복문을 써야한다.
total_height = 0
for height in student_heights: ## 동일한 리스트를 반복 수행하더라도 각 항목을 height 로 호출
total_height += height
print(total_height)
number_of_students = 0
for student in student_heights: ## 동일한 리스트를 반복 수행 각 항목을 학생수로 호출
number_of_students += 1
print(number_of_students)
avg = round(total_height/number_of_students)
print(avg)
'개인적인 공부 > Python' 카테고리의 다른 글
day 5-5 python (짝수 더하기) (0) | 2023.02.27 |
---|---|
day 5-4 for 반복문 과 range() (0) | 2023.02.27 |
day 5-1 for 반복문 (0) | 2023.02.25 |
day 4-7 python (가위 바위 보) (0) | 2023.02.24 |
day 4-5 python indexError 및 중첩리스트 (0) | 2023.02.24 |