본문 바로가기
python

[python]파이썬 for문 반복문

by jyee 2023. 11. 1.
728x90
반응형

 

 

 

문제 1

학생들의 평균키 구하기 

 

Example Input 1
156 178 165 171 187
In this case, student_heights would be a list that looks like: [156, 178, 165, 171, 187]
Example Output 1
total height = 857
number of students = 5
average height = 171

 

Example Input 2
151 145 179
Example Output 2
total height = 475
number of students = 3
average height = 158

# Input a Python list of student heights
student_heights = input().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 👇

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

num_of_student =0
for number in student_heights:
  num_of_student += 1
print(f"number of students = {num_of_student}")

average_height = round(total_height/num_of_student)
print(f"average height = {average_height}")

 

 

 

문제 2 

가장 큰 성적을 구하기 

Instructions
You are going to write a program that calculates the highest score from a List of scores.

e.g. student_scores = [78, 65, 89, 86, 55, 91, 64, 89]

Important you are not allowed to use the max or min functions. The output words must match the example. i.e

The highest score in the class is: x
Example Input
78 65 89 86 55 91 64 89
In this case, student_scores would be a list that looks like: [78, 65, 89, 86, 55, 91, 64, 89]

Example Output
The highest score in the class is: 91

 

max()함수를 쓰지 않고 오직 for문으로 구해야함 

# Input a list of student scores
student_scores = input().split()
for n in range(0, len(student_scores)):
  student_scores[n] = int(student_scores[n])

# Write your code below this row 👇
high_score= 0
for score in student_scores:
  if score > high_score:
    high_score = score
  
print(f"The highest score in the class is: {high_score}")

 

input list 순서대로  for문 score에 들어가고 그 다음 하나씩 비교하면서 숫자 중 가장 큰 값이 도출되는 것 

 

 

728x90
반응형

'python' 카테고리의 다른 글

[python]FizzBuzz 문제 풀기  (0) 2023.11.02
[Python] ADDING EVEN NUMBERS  (0) 2023.11.02
[python] BANKER ROULETTE 풀기  (0) 2023.10.27
[python]Heads or Tails 문제 풀기  (0) 2023.10.27
[python] DAY 3 - LOVE CALCULATOR 풀기  (1) 2023.10.26