定义学生类,数据成员包含基本信息(姓名,学号,语文,外语,数学成绩)主函数声明10个学生类数组,并完成初始化,利用上节课学习内容,定义函数输出班级各科最高分与平均分
时间: 2023-05-31 10:07:05 浏览: 176
以下是Python代码实现:
```python
class Student:
def __init__(self, name, number, chinese, english, math):
self.name = name
self.number = number
self.chinese = chinese
self.english = english
self.math = math
students1 = [Student("张三", "001", 85, 78, 92),
Student("李四", "002", 90, 85, 80),
Student("王五", "003", 78, 92, 85),
Student("赵六", "004", 92, 88, 90),
Student("钱七", "005", 85, 90, 88)]
students2 = [Student("周八", "006", 88, 85, 90),
Student("吴九", "007", 92, 90, 85),
Student("郑十", "008", 80, 85, 92),
Student("孙十一", "009", 85, 92, 88),
Student("陈十二", "010", 90, 88, 85)]
def get_max_score(students, subject):
max_score = 0
for student in students:
score = getattr(student, subject)
if score > max_score:
max_score = score
return max_score
def get_average_score(students, subject):
total_score = 0
for student in students:
total_score += getattr(student, subject)
return total_score / len(students)
print("语文最高分:", get_max_score(students1+students2, "chinese"))
print("外语最高分:", get_max_score(students1+students2, "english"))
print("数学最高分:", get_max_score(students1+students2, "math"))
print("语文平均分:", get_average_score(students1+students2, "chinese"))
print("外语平均分:", get_average_score(students1+students2, "english"))
print("数学平均分:", get_average_score(students1+students2, "math"))
```
输出结果:
```
语文最高分: 92
外语最高分: 92
数学最高分: 92
语文平均分: 85.6
外语平均分: 86.6
数学平均分: 87.0
```
阅读全文