Học sinh có điểm cao nhất
Thuộc phần Logic và luồng chương trình trong hành trình Python của Coddy. Bài học 45 trên 78.
Thử thách
DễTạo một hàm (function) có tên filter_top_students nhận một đối số: threshold (số thực). Hàm này cần:
- Duyệt qua từ điển (dictionary)
student_recordsvà tìm tất cả học sinh có điểm trung bình cao hơnthresholdđã chỉ định. - Dùng hàm
calculate_average_gradeđể lấy điểm trung bình của từng học sinh. - Trả về danh sách tên của những học sinh có điểm cao nhất.
- Nếu không có học sinh nào đáp ứng tiêu chí, trả về một danh sách rỗng.
Thêm (thay thế) khối code sau vào cuối code của bạn:
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 listHãy dành chút thời gian suy ngẫm về cách bạn kết hợp từ điển, tập hợp và logic ra quyết định để tạo ra một chương trình hoàn chỉnh. Làm tốt lắm! 🚀
Tự làm thử
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 listTất cả bài học trong Logic và luồng chương trình
4Ứng dụng sổ liên lạc
Hiển thị menuThêm liên hệ7Tập hợp phần 2
Phép toán toán học phần 1Phép toán toán học phần 2Ôn tập: Săn tìm kho báuTập hợp con và tập hợp chaLặp qua các tập hợpÔn tập: Theo dõi giải đấu10Tạo danh sách bằng cú pháp rút gọn cơ bản
Cú phápTạo danh sách đơn giảnThêm điều kiệnSử dụng phép tổng hợp dữ liệuÔn tập: Ngôi nhà của những danh sáchÔn tập: Những phần tử tự do13Hệ thống quản lý hàng tồn kho
Tổng quan dự ánThêm mặt hàng2Từ điển phần 1
Từ điển là gì?Tạo từ điểnTruy cập giá trịChỉnh sửa từ điểnÔn tập: Trình quản lý công thức nấu ăn5Ra quyết định nâng cao
Toán tử ba ngôiKiểm tra thành viênKiểm tra danh tínhLỗi thụt lềÔn tập: Bộ lọc kỳ nghỉ8Trình quản lý hồ sơ học sinh
Tổng quan dự ánThêm học sinh3Từ điển, phần 2
Phương thức của từ điểnTừ điển lồng nhauKiểm tra khóaLặp qua từ điểnÔn tập: Bộ đếm tần suất6Tập hợp phần 1
Tập hợp là gì?Các phép toán cơ bảnCác phương thức của tập hợpÔn tập: Xóa phần tử trùng lặp9Tổng hợp dữ liệu nâng cao
Sử dụng tổngTìm giá trị nhỏ nhất và lớn nhấtSắp xếp dữ liệu hiệu quảÔn tập: Bộ sắp xếp từ điểnTự luyện tập: Trình biên dịch Python online