Top Students
Part of the Logic & Flow section of Coddy's Python journey — lesson 45 of 78.
Challenge
EasyCreate a function named filter_top_students that takes one argument: threshold (float). The function should:
- Iterate through the
student_recordsdictionary and find all students whose average grade is greater than the specifiedthreshold. - Use the
calculate_average_gradefunction to get each student's average grade. - Return a list of names of the top students.
- If no students meet the criteria, return an empty list.
Add (replace) the following block of code at the bottom of your code:
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 listTake a moment to reflect on how you’ve combined dictionaries, sets, and decision-making to create a fully functional program. Great job! 🚀
Try it yourself
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 listAll lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter