Menu
Coddy logo textTech

등록 여부 확인

Coddy Python 여정의 Logic & Flow 섹션에 포함된 레슨 — 78개 중 42번째.

challenge icon

챌린지

쉬움

name (문자열)과 course (문자열) 두 개의 인자를 받는 is_enrolled라는 이름의 함수를 만드세요. 이 함수는 다음을 수행해야 합니다:

  1. student_records 딕셔너리에 학생 이름이 존재하는지 확인합니다.
    • 존재하지 않는 경우, "Student '<name>' not found."를 출력하고 False를 반환합니다.
  2. 이름이 존재하면, course가 해당 학생의 courses 세트(set)에 있는지 확인합니다.
    • 있다면 True를 반환합니다.
    • 없다면 False를 반환합니다.

코드의 맨 아래에 다음 코드 블록을 추가(또는 교체)하세요:

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(is_enrolled("Alice", "Math"))  # Should return True
print(is_enrolled("Alice", "Biology"))  # Should return False
print(is_enrolled("Bob", "Biology"))  # Should return True
print(is_enrolled("Charlie", "Math"))  # Non-existent student, should print message and return False

직접 해보기

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.")

def add_grade(name, grade):
    if name not in student_records:
        print(f"Student '{name}' not found.")
        return
    student_records[name]["grades"].add(grade)
    print(f"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)

Logic & Flow의 모든 레슨