성적 추가
Coddy Python 여정의 Logic & Flow 섹션에 포함된 레슨 — 78개 중 41번째.
챌린지
쉬움name (문자열)과 grade (정수)라는 두 개의 인자를 받는 add_grade라는 이름의 함수를 만드세요. 이 함수는 다음을 수행해야 합니다:
student_records딕셔너리에 해당 학생 이름이 존재하는지 확인합니다.- 존재하지 않는 경우,
"Student '<name>' not found."를 출력합니다.
- 존재하지 않는 경우,
- 이름이 존재하면, 해당 학생의
grades세트(set)에grade를 추가합니다. "Grade <grade> added for student '<name>'."를 출력합니다.
코드의 맨 아래에 다음 코드 블록을 추가(교체)하세요:
add_student("Alice", 20, ["Math", "Physics"])
add_student("Bob", 22, ["Biology", "Chemistry"])
add_grade("Alice", 90)
add_grade("Alice", 85)
add_grade("Bob", 75)
add_grade("Charlie", 80) # Non-existent student
print(student_records)직접 해보기
student_records = {}
def add_student(name, age, courses):
if name in student_records:
print(f"Student '{name}' already exists.")
return
student_records[name] = {"age": age, "grades": set(), "courses": set(courses)}
print(f"Student '{name}' added successfully.")
add_student("Alice", 20, ["Math", "Physics"])
add_student("Bob", 22, ["Biology", "Chemistry"])
print(student_records)