定义学生类,输入同学的数学英语计算机成绩,输出,并可以输入学号搜索信息,最后算出全班总成绩最高分和平均分
时间: 2024-05-07 16:17:53 浏览: 75
设计一个学生类Student(学生学号、姓名、数学、英语、计算机成绩;)
5星 · 资源好评率100%
好的,以下是一个简单的Python代码示例,实现了您所需的功能:
```python
class Student:
def __init__(self, id, math_score, english_score, computer_score):
self.id = id
self.math_score = math_score
self.english_score = english_score
self.computer_score = computer_score
def display(self):
print("学号:", self.id)
print("数学成绩:", self.math_score)
print("英语成绩:", self.english_score)
print("计算机成绩:", self.computer_score)
@staticmethod
def search(student_list, id):
for student in student_list:
if student.id == id:
student.display()
return
print("未找到该学号的学生信息!")
@staticmethod
def get_class_score_stats(student_list):
total_score = 0
max_score = 0
for student in student_list:
total_score += student.math_score + student.english_score + student.computer_score
max_score = max(max_score, student.math_score + student.english_score + student.computer_score)
avg_score = total_score / len(student_list) / 3
return max_score, avg_score
# 示例用法:
if __name__ == "__main__":
student_list = [
Student(1, 90, 80, 70),
Student(2, 80, 70, 60),
Student(3, 85, 90, 95),
]
# 显示所有学生信息
for student in student_list:
student.display()
# 搜索学号为2的学生信息
Student.search(student_list, 2)
# 显示全班总成绩最高分和平均分
max_score, avg_score = Student.get_class_score_stats(student_list)
print("全班总成绩最高分:", max_score)
print("全班平均分:", avg_score)
```
需要注意的是,这只是一个简单的示例代码,实际的程序可能需要更详细的输入、输出、错误处理等机制。
阅读全文