문제 2. 학생 성적 관리 시스템
- 다음 요구사항에 맞게 3개의 함수를 작성하세요.
- 학생들의 이름과 성적을 딕셔너리에 저장하고, 출력하는 함수를 작성하세요. (add_student(grades, name, score))
- 성적 평균을 반환하는 함수를 작성하세요. (calculate_average(grades))
- 최고 점수 학생을 반환하는 프로그램 작성하세요. (find_top_students(grades))
- 출력결과를 참고하여, 같은 양식으로 출력되도록 함수를 작성하세요.
- 힌트: 딕셔너리 자료형에서 max와 min값 구하는 방법에 대해 필요한 경우, 아래 링크를 참고하세요. https://note.nkmk.me/en/python-dict-value-max-min/
Skeleton code
def add_student(grades, name, score):
"""
코드 작성
"""
def calculate_average(grades):
"""
코드 작성
"""
def find_top_student(grades):
"""
코드 작성
"""
# Example usage
grades = {}
add_student(grades, "Alice", 85)
add_student(grades, "Bob", 92)
add_student(grades, "Charlie", 78)
print(f"Average score: {calculate_average(grades):.2f}")
top_student, top_score = find_top_student(grades)
print(f"Top student: {top_student} with score {top_score}")
출력 결과 예시
"""
Added Alice with score 85.
Added Bob with score 92.
Added Charlie with score 78.
Average score: 85.00
Top student: Bob with score 92
"""
💡 문제 풀이
`문제 풀이`
def add_student(grades, name, score):
grades[name] = score
print(f"Added {name} with score {score}.")
def calculate_average(grades):
return sum(grades.values())/len(grades)
def find_top_student(grades):
top_score = max(grades.values())
top_student = max(grades)
return top_student, top_score
# Example usage
grades = {}
add_student(grades, "Alice", 85)
add_student(grades, "Bob", 92)
add_student(grades, "Charlie", 78)
print(f"Average score: {calculate_average(grades):.2f}")
top_student, top_score = find_top_student(grades)
print(f"Top student: {top_student} with score {top_score}")
`오답노트`
- 뒤에 있던 grades = { } 코드를 뒤늦게 봐서 처음에 좀 헤맸다. 처음부터 문제 전체적으로 변수들 꼼꼼하게 확인하기