用python简一个写学生管理系统
时间: 2024-05-29 12:06:02 浏览: 66
当然可以!以下是一个简单的学生管理系统的Python代码例:
```python
class Student:
definit__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
class StudentManagementSystem:
def __init__(self):
self.students = []
def add_student(self, name, age, grade):
student = Student(name, age, grade)
self.students.append(student)
def remove_student(self, name):
for student in self.students:
if student.name == name:
self.students.remove(student)
return True
return False
def get_student_info(self, name):
for student in self.students:
if student.name == name:
return f"Name: {student.name}, Age: {student.age}, Grade: {student.grade}"
return "Student not found"
# 创建学生管理系统对象
sms = StudentManagementSystem()
# 添加学生
sms.add_student("Alice", 18, 12)
sms.add_student("Bob", 17, 11)
# 获取学生信息
print(sms.get_student_info("Alice")) # 输出:Name: Alice, Age: 18, Grade: 12
# 移除学生
sms.remove_student("Bob")
# 再次获取学生信息
print(sms.get_student_info("Bob")) # 输出:Student not found
```
这个学生管理系统使用了两个类:`Student`表示学生对象,`StudentManagementSystem`表示学生管理系统。你可以通过调用`add_student`方法添加学生,通过调用`remove_student`方法移除学生,通过调用`get_student_info`方法获取学生信息。
阅读全文