成绩管理系统python
时间: 2023-07-08 19:53:06 浏览: 32
以下是一个简单的成绩管理系统的Python代码示例:
```
# 定义学生类
class Student:
def __init__(self, name, id):
self.name = name
self.id = id
self.scores = []
def add_score(self, score):
self.scores.append(score)
def get_average(self):
return sum(self.scores) / len(self.scores)
# 定义管理系统类
class GradeManagementSystem:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def get_student(self, id):
for student in self.students:
if student.id == id:
return student
return None
def get_average_score(self):
total_score = 0
total_count = 0
for student in self.students:
total_score += sum(student.scores)
total_count += len(student.scores)
return total_score / total_count
# 主程序
if __name__ == '__main__':
system = GradeManagementSystem()
# 添加学生
system.add_student(Student('张三', '2018001'))
system.add_student(Student('李四', '2018002'))
system.add_student(Student('王五', '2018003'))
# 添加成绩
system.get_student('2018001').add_score(80)
system.get_student('2018001').add_score(90)
system.get_student('2018002').add_score(75)
system.get_student('2018002').add_score(85)
system.get_student('2018003').add_score(90)
system.get_student('2018003').add_score(95)
# 输出平均分和每个学生的平均分
print('总平均分:', system.get_average_score())
for student in system.students:
print(student.name, '的平均分:', student.get_average())
```
此代码实现了一个简单的成绩管理系统,其中包括了学生类和管理系统类。学生类包含了学生的基本信息和成绩信息,管理系统类包含了学生集合和相关操作。主程序中演示了如何添加学生和成绩,并输出每个学生的平均分和总平均分。实际应用中,可以根据需求对代码进行适当的修改和扩展。