有一个班4个学生,5门课。①求第1门课的平均分;②找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩和平均成绩;③找出平均成绩在90分以上或全部课程成绩在85分以上的学生。分别编3个函数实现以上3个要求。
时间: 2024-12-14 15:26:20 浏览: 10
c代码-五个学生考五科,求平均分最高的为多少分并求出其序号(行可控制0-6)
首先,我们可以定义三个函数来满足这三个需求。为了简化问题,我们假设分数都是整数,并且学生的课程成绩存储在一个二维数组或列表中,其中每个元素包含学生ID和对应课程的成绩。
```python
def calculate_average_score(grades, course_index):
total = sum(grade for student_grades in grades for grade in student_grades[course_index])
return total / len(grades)
def find_students_below_threshold(grades):
below_two_courses = []
for i, student_grades in enumerate(grades):
if sum(student_grades.count(score) for score in range(60, 70)) >= 2: # 及格线设为60分
below_two_courses.append((i, {course: grade for course, grade in enumerate(student_grades)}))
return below_two_courses
def filter_students_with_higher_average(grades):
high_scores = []
for i, student_grades in enumerate(grades):
average = calculate_average_score(student_grades)
if average >= 90 or all(grade >= 85 for grade in student_grades):
high_scores.append((i, student_grades))
return high_scores
# 示例数据:
students_grades = [
[80, 95, 88, 70, 92],
[75, 82, 78, 60, 89],
[95, 98, 90, 80, 85], # 可能是优秀学生
[72, 77, 75, 65, 80] # 可能不及格
]
# 使用函数:
average_first_course = calculate_average_score(students_grades, 0)
students_below_two_courses = find_students_below_threshold(students_grades)
high_scoring_students = filter_students_with_higher_average(students_grades)
print(f"第1门课的平均分:{average_first_course}")
print("两门以上不及格的学生信息:")
for student, scores in students_below_two_courses:
print(f"学号:{student}, 成绩:{scores}, 平均成绩:{calculate_average_score(scores.values())}")
print("\n平均成绩在90分以上或全部课程成绩在85分以上的学生:")
for student, scores in high_scoring_students:
print(f"学号:{student}, 成绩:{scores}, 平均成绩:{calculate_average_score(scores.values())}")
阅读全文