编写代码:某班有4个学生,共5门课程成绩。编程实现: (1)求每门课程成绩的平均分; (2)求每个学生的全部课程成绩的平均分。
时间: 2024-05-09 13:18:32 浏览: 21
Sure, I can help you with that! Here's a Python code that can solve your problem:
```python
# define the grades of each student
student_grades = [
[90, 80, 85, 92, 88],
[80, 75, 70, 68, 74],
[95, 98, 92, 97, 91],
[85, 82, 91, 88, 90]
]
# calculate the average grade of each subject
subject_averages = []
for subject_grades in zip(*student_grades):
subject_averages.append(sum(subject_grades) / len(subject_grades))
# print the subject averages
for i, average in enumerate(subject_averages):
print("Average grade for subject {}: {:.2f}".format(i+1, average))
# calculate the average grade of each student
student_averages = []
for student_grades in student_grades:
student_averages.append(sum(student_grades) / len(student_grades))
# print the student averages
for i, average in enumerate(student_averages):
print("Average grade for student {}: {:.2f}".format(i+1, average))
```
Please note that this code assumes that the grades are stored in a nested list `student_grades`, where each inner list represents the grades of a single student. Also, the output is rounded to two decimal places using the `format()` method. Hope this helps!