用python写一个成绩管理系统
时间: 2023-05-23 11:02:54 浏览: 52
好的,我可以为您提供一个简单的成绩管理系统的Python代码示例:
```
class Student:
def __init__(self, name, math=0, english=0, science=0):
self.name = name
self.math = math
self.english = english
self.science = science
class Gradebook:
def __init__(self):
self.students = {}
def add_student(self, student):
self.students[student.name] = student
def remove_student(self, name):
del self.students[name]
def get_student(self, name):
return self.students.get(name)
def get_all_students(self):
return self.students.values()
def main():
gb = Gradebook()
# Add students
gb.add_student(Student("Alice", 90, 85, 95))
gb.add_student(Student("Bob", 80, 70, 75))
gb.add_student(Student("Charlie", 95, 90, 100))
# Print all students
print("All students:")
for student in gb.get_all_students():
print(f"{student.name}: Math = {student.math}, English = {student.english}, Science = {student.science}")
# Remove a student
gb.remove_student("Bob")
# Get a student by name
student = gb.get_student("Charlie")
if student:
print(f"Charlie's grades: Math = {student.math}, English = {student.english}, Science = {student.science}")
else:
print("Charlie not found")
if __name__ == "__main__":
main()
```
这是一个简单的成绩管理系统,包括两个类:`Student` 和 `Gradebook`。`Student` 类表示一个学生,包含姓名和三个科目的成绩;`Gradebook` 类表示整个班级的成绩册,包含所有学生的信息和管理功能。在 `main` 函数中,我们创建了一个 `Gradebook` 对象,添加了三个学生的信息,并显示了所有学生的成绩。我们还演示了如何删除一个学生和如何通过姓名获取一个学生的信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)