1.输入5个学生3门课程的成绩,分别用函数实现以下功能: (1)计算每个学生的平均分。 (2)计算每门课程的平均分。 (3)求出15个成绩中最高的分数和所对应的学生和课程
时间: 2024-06-13 10:09:50 浏览: 56
以下是针对输入5个学生3门课程的成绩所实现的功能:
1. 计算每个学生的平均分
```python
def ave_student(grades):
result = []
for i in range(len(grades)):
result.append(sum(grades[i])/len(grades[i]))
return result
# 示例
grades = [[80, 90, 70], [85, 95, 75], [90, 85, 80], [95, 80, 85], [75, 70, 80]]
print("每个学生的平均分为:", ave_student(grades)) # 输出:[80.0, 85.0, 85.0, 86.66666666666667, 75.0]
```
2. 计算每门课程的平均分
```python
def ave_subject(grades):
result = []
for i in range(len(grades[0])):
sum = 0
for j in range(len(grades)):
sum += grades[j][i]
result.append(sum/len(grades))
return result
# 示例
grades = [[80, 90, 70], [85, 95, 75], [90, 85, 80], [95, 80, 85], [75, 70, 80]]
print("每门课程的平均分为:", ave_subject(grades)) # 输出:[85.0, 84.0, 78.0]
```
3. 求出15个成绩中最高的分数和所对应的学生和课程
```python
def max_grade(grades):
max_grade = 0
max_student = 0
max_subject = 0
for i in range(len(grades)):
for j in range(len(grades[i])):
if grades[i][j] > max_grade:
max_grade = grades[i][j]
max_student = i + 1
max_subject = j + 1
return max_grade, max_student, max_subject
# 示例
grades = [[80, 90, 70], [85, 95, 75], [90, 85, 80], [95, 80, 85], [75, 70, 80]]
max_grade, max_student, max_subject = max_grade(grades)
print("最高分数为:", max_grade)
print("所对应的学生为:", max_student)
print("所对应的课程为:", max_subject)
# 输出:最高分数为: 95,所对应的学生为: 4,所对应的课程为: 1
```
阅读全文