Menu
Coddy logo textTech

Studenti migliori

Fa parte della sezione Logica e flusso del percorso Python di Coddy. Lezione 45 di 78.

challenge icon

Sfida

Facile

Crea una funzione chiamata filter_top_students che accetta un argomento: threshold (float). La funzione dovrebbe:

  1. Scorrere il dizionario student_records e trovare tutti gli studenti la cui media dei voti è superiore al valore specificato di threshold.
  2. Usare la funzione calculate_average_grade per ottenere la media dei voti di ogni studente.
  3. Restituire un elenco dei nomi degli studenti migliori.
  4. Se nessuno studente soddisfa i criteri, restituire un elenco vuoto.

Aggiungi (sostituisci) il seguente blocco di codice in fondo al tuo codice:

add_student("Alice", 20, ["Math", "Physics"])
add_student("Bob", 22, ["Math", "Biology"])
add_student("Diana", 23, ["Chemistry", "Physics"])
add_grade("Alice", 90)
add_grade("Alice", 85)
add_grade("Bob", 75)
add_grade("Diana", 95)
print(filter_top_students(80))  # Should return ["Alice", "Diana"]
print(filter_top_students(90))  # Should return ["Diana"]
print(filter_top_students(100))  # Should return an empty list

Prenditi un momento per riflettere su come hai combinato dizionari, insiemi e processi decisionali per creare un programma completamente funzionante. Ottimo lavoro! 🚀

Provalo tu

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

def is_enrolled(name, course):
    if name not in student_records:
        print(f"Student '{name}' not found.")
        return False
    return course in student_records[name]["courses"]

def calculate_average_grade(name):
    if name not in student_records:
        print(f"Student '{name}' not found.")
        return None
    grades = student_records[name]["grades"]
    if not grades:
        return 0
    return sum(grades) / len(grades)

def list_students_by_course(course):
    students_in_course = []
    for name, details in student_records.items():
        if course in details["courses"]:
            students_in_course.append(name)
    return students_in_course


add_student("Alice", 20, ["Math", "Physics"])
add_student("Bob", 22, ["Math", "Biology"])
add_student("Diana", 23, ["Chemistry", "Physics"])
print(list_students_by_course("Math"))  # Should return ["Alice", "Bob"]
print(list_students_by_course("Physics"))  # Should return ["Alice", "Diana"]
print(list_students_by_course("Biology"))  # Should return ["Bob"]
print(list_students_by_course("History"))  # Should return an empty list

Tutte le lezioni di Logica e flusso

Esercitati da solo: Compilatore Python online