Menu
CoddyTech

등록 여부 확인

Coddy Python 여정의 논리와 흐름 섹션에 포함된 레슨. 78개 중 42번째.

challenge icon

챌린지

쉬움

is_enrolled라는 함수를 만들어 두 개의 인수를 받도록 하세요. 인수는 name(문자열)과 course(문자열)입니다. 함수는 다음을 수행해야 합니다.

  1. 학생 이름이 student_records 딕셔너리에 있는지 확인합니다.
    • 없으면 "Student '<name>' not found."를 출력하고 False를 반환합니다.
  2. 이름이 있으면 해당 학생의 courses 집합에 course가 있는지 확인합니다.
    • 있으면 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)

논리와 흐름의 모든 레슨

직접 연습해 보세요: 온라인 Python 컴파일러