有5个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入5个学生的数据,要求输出3门课总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课的成绩、平均分数)
时间: 2024-01-06 08:06:54 浏览: 127
以下是一个实现你所描述功能的Python代码:
```python
# 定义学生类
class Student:
def __init__(self, student_id, name, scores):
self.student_id = student_id
self.name = name
self.scores = scores
def get_average_score(self):
return sum(self.scores) / len(self.scores)
# 创建学生列表
students = []
for i in range(5):
student_id = input("请输入学生学号:")
name = input("请输入学生姓名:")
scores = []
for j in range(3):
score = float(input("请输入第{}门课的成绩:".format(j+1)))
scores.append(score)
student = Student(student_id, name, scores)
students.append(student)
# 计算总平均成绩
total_average_score = sum([student.get_average_score() for student in students]) / len(students)
# 找到最高分的学生
max_score_student = max(students, key=lambda student: student.get_average_score())
# 输出结果
print("3门课的总平均成绩:", total_average_score)
print("最高分的学生信息:")
print("学号:", max_score_student.student_id)
print("姓名:", max_score_student.name)
print("成绩:", max_score_student.scores)
print("平均分数:", max_score_student.get_average_score())
```
阅读全文