用python语言写一个学生管理系统代码
时间: 2023-12-15 20:07:17 浏览: 131
以下是一个简单的学生管理系统的Python代码:
```python
class Student:
def __init__(self, name, id_number):
self.name = name
self.id_number = id_number
self.courses = []
def add_course(self, course):
self.courses.append(course)
def remove_course(self, course):
self.courses.remove(course)
class Course:
def __init__(self, name, code):
self.name = name
self.code = code
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, student):
self.students.remove(student)
class StudentManagementSystem:
def __init__(self):
self.students = []
self.courses = []
def add_student(self, name, id_number):
student = Student(name, id_number)
self.students.append(student)
return student
def remove_student(self, student):
self.students.remove(student)
def add_course(self, name, code):
course = Course(name, code)
self.courses.append(course)
return course
def remove_course(self, course):
self.courses.remove(course)
def enroll_student(self, student, course):
student.add_course(course)
course.add_student(student)
def withdraw_student(self, student, course):
student.remove_course(course)
course.remove_student(student)
def print_students(self):
print("Students:")
for student in self.students:
print(f"{student.name} ({student.id_number})")
def print_courses(self):
print("Courses:")
for course in self.courses:
print(f"{course.name} ({course.code})")
def print_enrollments(self):
print("Enrollments:")
for course in self.courses:
print(f"{course.name} ({course.code}):")
for student in course.students:
print(f" {student.name} ({student.id_number})")
# Example usage:
sms = StudentManagementSystem()
# Add students and courses
alice = sms.add_student("Alice", "123456")
bob = sms.add_student("Bob", "789012")
math = sms.add_course("Math", "MATH101")
english = sms.add_course("English", "ENGL101")
# Enroll students in courses
sms.enroll_student(alice, math)
sms.enroll_student(bob, math)
sms.enroll_student(bob, english)
# Print information
sms.print_students()
sms.print_courses()
sms.print_enrollments()
# Withdraw student from course
sms.withdraw_student(bob, math)
# Print updated information
sms.print_enrollments()
```
这个学生管理系统包含了学生和课程两个类,以及一个学生管理系统类。你可以通过这个系统添加、移除学生和课程,以及将学生注册到课程中,退选课程等等。在这个系统中,你可以打印出学生和课程的信息,以及所有学生的注册信息。
阅读全文