输入5个学生的数学,语文,英语三门课程的成绩,计算并输出每一门课程的平均成绩和每一位学生的平均成绩
时间: 2024-12-24 15:17:10 浏览: 14
当然可以。首先,我们需要定义一个数据结构来存储每个学生的信息,比如一个包含姓名和成绩的字典。假设我们有以下五位学生的数据:
```python
students = {
'Student1': {'math': 85, 'chinese': 90, 'english': 88},
'Student2': {'math': 92, 'chinese': 95, 'english': 90},
'Student3': {'math': 78, 'chinese': 82, 'english': 76},
'Student4': {'math': 96, 'chinese': 98, 'english': 92},
'Student5': {'math': 80, 'chinese': 84, 'english': 83}
}
```
接下来,我们可以编写函数来计算每门课程的平均分以及每位学生的总平均分:
```python
def calculate_averages(students):
# 每门课程的总分
math_total = sum(student['math'] for student in students.values())
chinese_total = sum(student['chinese'] for student in students.values())
english_total = sum(student['english'] for student in students.values())
# 每门课程的学生人数
num_students = len(students)
# 计算平均分
math_avg = math_total / num_students
chinese_avg = chinese_total / num_students
english_avg = english_total / num_students
# 每位学生的平均分
individual_student_averages = {student: {subject: score / num_students for subject, score in student.items()}
for student, scores in students.items()}
print(f"Math Average: {math_avg}")
print(f"Chinese Average: {chinese_avg}")
print(f"English Average: {english_avg}")
for student, scores in individual_student_averages.items():
print(f"{student}'s Overall Average: {sum(scores.values())/len(scores)}")
# 执行计算
calculate_averages(students)
```
运行这个脚本会得到每门课程的平均分和每位学生的平均分。
阅读全文