python
python (반복문while, list)
멈머이
2023. 11. 9. 00:30
728x90
7명의 심판의 점수를 받아서 최소, 최댓값을 뺴는 프로그램
최소, 최댓값이 빠진 점수의 평균구하기
*요구사항
7멍의 심판들의 점수를 입력받아 리스트에 저장
7번의 input필요
입력값을 리스트에 저장
최대 최소값 찾기
평균 구하기
scores = list()
count = 0
while count < 7:
count += 1
point = input(f'{count}번째 심사위원 점수를 입력해 주세요:')
# if point.isdigit():
if point.replace('.','').isdecimal():
scores.append(float(point))
min = min(scores)
max = max(scores)
scores.remove(min)
scores.remove(max)
print(min, max)
print(scores)
avg = sum(scores)/len(scores)
print(f'평균은 : {avg}입니다.')

*코드설명
scores = list()
변수 scores는 list이다.
count = 0
while count < 7:
count += 1
point = input(f'{count}번째 심사위원 점수를 입력해 주세요:')
변수 count를 0으로 초기화해 주고,
count가 7이 될 때까지 +1씩 올라가며 input에 데이터를 받는다.
if point.replace('.','').isdecimal():
scores.append(float(point))
점수는 숫자만 입력받도록 하기 위해 replace()를 사용하여 소수점 기호를 제거한 후 isdecimal()로 숫자인지 확인한다.
숫자인 경우 float()를 사용하여 float 형으로 변환한 후 scores 리스트에 추가한다.
min = min(scores)
max = max(scores)
scores.remove(min)
scores.remove(max)
print(min, max)
score내부의 최대, 최솟값을 찾고 지워준다.
print(scores)
avg = sum(scores)/len(scores)
print(f'평균은 : {avg}입니다.')
최대, 최솟값이 제거된 점수를 보여주며, 그 점수의 평균을 보여준다.
728x90