提供Python编写一个学生成绩管理系统代码
时间: 2024-02-12 20:07:33 浏览: 73
好的,以下是一个简单的Python学生成绩管理系统的代码示例:
```python
class Student:
def __init__(self, name, score):
self.name = name
self.score = score
class Gradebook:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, student):
self.students.remove(student)
def print_students(self):
for student in self.students:
print("Name: {}, Score: {}".format(student.name, student.score))
def get_highest_score(self):
highest_score = 0
for student in self.students:
if student.score > highest_score:
highest_score = student.score
return highest_score
def get_average_score(self):
total_score = 0
for student in self.students:
total_score += student.score
return total_score / len(self.students)
# Example usage
gradebook = Gradebook()
student1 = Student("Alice", 85)
student2 = Student("Bob", 72)
student3 = Student("Charlie", 90)
gradebook.add_student(student1)
gradebook.add_student(student2)
gradebook.add_student(student3)
print("All Students:")
gradebook.print_students()
highest_score = gradebook.get_highest_score()
print("Highest Score: {}".format(highest_score))
average_score = gradebook.get_average_score()
print("Average Score: {}".format(average_score))
```
该代码使用了两个类:`Student` 和 `Gradebook`。 `Student` 类包含学生的名称和分数, `Gradebook` 类管理所有学生的列表,并提供添加、删除和打印学生信息的方法,以及获取最高分和平均分的方法。在示例中,我们创建了三个学生,将其添加到成绩簿中,并打印了所有学生的信息、最高分和平均分。
阅读全文