3、有 5 个学生,每个学生的数据包括学号、姓名、3 门课程的 成绩,从键盘输入 10个学生数据,要求输出 3门课程总平均成绩,以及最高分的学生的数据(包括学号、姓名、3 门课程成绩 平均分数)。 【要求】用一个input函数输入5个学生数据,用一个average函数求总平均分,用 max 函数找出最高分学生数据。总平均分和最高分的学生的数据都在主函数中输出。
时间: 2023-12-15 16:43:50 浏览: 78
好的,这是一个关于学生成绩的问题,以下是代码实现:
```python
def input_student():
students = []
for i in range(5):
stu_id = input("请输入第{}个学生的学号:".format(i+1))
stu_name = input("请输入第{}个学生的姓名:".format(i+1))
scores = []
for j in range(3):
score = float(input("请输入{}的第{}门课成绩:".format(stu_name, j+1)))
scores.append(score)
student = {'id': stu_id, 'name': stu_name, 'scores': scores}
students.append(student)
return students
def average(students):
total_scores = [0, 0, 0]
for student in students:
total_scores[0] += student['scores'][0]
total_scores[1] += student['scores'][1]
total_scores[2] += student['scores'][2]
avg_scores = [total_scores[0]/5, total_scores[1]/5, total_scores[2]/5]
return avg_scores
def find_max_stu(students):
max_score = 0
max_stu = None
for student in students:
avg_score = sum(student['scores'])/3
if avg_score > max_score:
max_score = avg_score
max_stu = student
return max_stu
if __name__ == '__main__':
students = input_student()
avg_scores = average(students)
print("3门课程的总平均成绩为:", avg_scores)
max_stu = find_max_stu(students)
print("最高分学生的数据为:")
print("学号:", max_stu['id'])
print("姓名:", max_stu['name'])
print("3门课程成绩:", max_stu['scores'])
print("平均分数:", sum(max_stu['scores'])/3)
```
在这个程序中,我们首先定义了一个 `input_student` 函数,用于输入 5 个学生的学号、姓名和成绩。然后我们定义了一个 `average` 函数,用于计算 3 门课程的总平均成绩。最后我们定义了一个 `find_max_stu` 函数,用于找出最高分的学生的数据。
在主函数中,我们首先调用 `input_student` 函数输入学生数据,然后调用 `average` 函数求总平均成绩,并输出结果。接着我们调用 `find_max_stu` 函数找出最高分的学生,并输出学生数据和平均分数。
阅读全文
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)