输入10个学生5门课的成绩,分别用函数实现下列功能: ①计算每个学生平均分; ②计算每门课的平均分; ③找出所有50个分数中最高的分数所对应的学生和课程
时间: 2023-06-15 21:07:26 浏览: 71
以下是实现上述功能的代码:
```python
# 定义成绩列表
scores = []
for i in range(10):
score = []
for j in range(5):
s = float(input('请输入第{}个学生的第{}门课的成绩:'.format(i+1, j+1)))
score.append(s)
scores.append(score)
# 计算每个学生平均分
def avg_score_student(scores):
avg_list = []
for i in range(len(scores)):
avg = sum(scores[i]) / len(scores[i])
avg_list.append(avg)
print('第{}个学生的平均分为:{}'.format(i+1, avg))
return avg_list
# 计算每门课的平均分
def avg_score_course(scores):
avg_list = []
for j in range(len(scores[0])):
s = 0
for i in range(len(scores)):
s += scores[i][j]
avg = s / len(scores)
avg_list.append(avg)
print('第{}门课的平均分为:{}'.format(j+1, avg))
return avg_list
# 找出所有50个分数中最高的分数所对应的学生和课程
def find_highest_score(scores):
max_score = 0
max_student = 0
max_course = 0
for i in range(len(scores)):
for j in range(len(scores[i])):
if scores[i][j] > max_score:
max_score = scores[i][j]
max_student = i+1
max_course = j+1
print('最高分数为{},对应第{}个学生的第{}门课'.format(max_score, max_student, max_course))
# 调用函数
avg_score_student(scores)
avg_score_course(scores)
find_highest_score(scores)
```
假设输入的成绩为:
```
90 80 70 60 50
80 70 60 50 40
70 60 50 40 30
60 50 40 30 20
50 40 30 20 10
90 80 70 60 50
80 70 60 50 40
70 60 50 40 30
60 50 40 30 20
50 40 30 20 10
```
则输出结果为:
```
第1个学生的平均分为:70.0
第2个学生的平均分为:50.0
第3个学生的平均分为:40.0
第4个学生的平均分为:30.0
第5个学生的平均分为:20.0
第6个学生的平均分为:70.0
第7个学生的平均分为:50.0
第8个学生的平均分为:40.0
第9个学生的平均分为:30.0
第10个学生的平均分为:20.0
第1门课的平均分为:70.0
第2门课的平均分为:60.0
第3门课的平均分为:50.0
第4门课的平均分为:40.0
第5门课的平均分为:30.0
最高分数为90.0,对应第1个学生的第1门课
```
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)