描述: 按照输入输出格式要求,根据程序模板将代码补充完整。求出Student数组中学生的最高平均分和最低平均分,并从高分到低分对数组重新排序,输出排序后的学生信息。 输入 先输入3个课程号,然后逐次输入4位学生的信息。 输出 按照格式输出最高分,最低分,以及排序后的结果。 示例: 输入: 201 202 203 1 zhang 67 88 92 2 li 44 77 99 3 wang 76 82 95 4 zhao 63 82 86 输出: Student wang got the highest average score as 84.3333 Student li got the lowest average score as 73.3333 Student id:3 Student name:wang lesson_id 201 202 203 Average scores 76 82 95 84.3333
时间: 2024-02-14 10:35:07 浏览: 20
以下是代码模板,已经给出了一些部分的实现。你需要根据注释和输入输出要求补充代码。
```python
class Student:
def __init__(self, id, name, scores):
self.id = id
self.name = name
self.scores = scores
def get_average_score(self):
# 计算平均分
return sum(self.scores) / len(self.scores)
if __name__ == '__main__':
lesson_ids = list(map(int, input().split())) # 输入3个课程号,转化为整数列表
students = []
for i in range(4):
id, name = input().split() # 输入学生的id和名字
scores = list(map(int, input().split())) # 输入学生成绩,转化为整数列表
student = Student(id, name, scores)
students.append(student)
highest_average_score = 0
lowest_average_score = float('inf')
for student in students:
average_score = student.get_average_score()
# 更新最高分和最低分
highest_average_score = max(highest_average_score, average_score)
lowest_average_score = min(lowest_average_score, average_score)
# 输出最高分和最低分
print("Student {} got the highest average score as {:.4f}".format(____, ____))
print("Student {} got the lowest average score as {:.4f}".format(____, ____))
# 对学生列表进行排序
students.sort(key=lambda x: x.get_average_score(), reverse=True)
# 输出排序后的学生信息
for student in students:
print("Student id: {} Student name:{} lesson_id {} {} {} Average scores {:.0f} {:.0f} {:.0f} {:.4f}".format(
student.id, student.name, lesson_ids[0], lesson_ids[1], lesson_ids[2], student.scores[0], student.scores[1], student.scores[2], student.get_average_score()))
```
在代码模板中,需要你完成以下部分的代码:
1. 第10行,使用 `input()` 函数输入学生的id和名字,并赋值给 `id` 和 `name` 变量。
2. 第11行,使用 `input()` 函数输入学生成绩,并转化为整数列表,赋值给 `scores` 变量。
3. 第24行,使用字符串的 `format()` 方法输出最高分和最低分。其中,需要填入的内容分别是最高分和最低分对应的学生的名字和平均分,保留四位小数。格式为 `"Student {} got the highest average score as {:.4f}"` 和 `"Student {} got the lowest average score as {:.4f}"`。
4. 第27行,使用字符串的 `format()` 方法输出每个学生的信息。其中,需要填入的内容分别是学生的id、名字、三门课的编号、三门课的成绩和平均分。格式为 `"Student id: {} Student name:{} lesson_id {} {} {} Average scores {:.0f} {:.0f} {:.0f} {:.4f}"`。
注意,输入输出格式必须与题目描述中的一致!