使用Python设计一个学生管理系统
时间: 2023-11-17 09:03:31 浏览: 62
基于Python的校园学生一卡通管理系统.zip
5星 · 资源好评率100%
好的,以下是一个简单的学生管理系统的Python代码:
```python
class Student:
def __init__(self, name, age, score):
self.name = name
self.age = age
self.score = score
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
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 modify_student(self, name, age, score):
for student in self.students:
if student.name == name:
student.age = age
student.score = score
return True
return False
def search_student(self, name):
for student in self.students:
if student.name == name:
return student
return None
def show_all_students(self):
for student in self.students:
print("姓名:{},年龄:{},成绩:{}".format(student.name, student.age, student.score))
if __name__ == "__main__":
manager = StudentManager()
while True:
print("1. 添加学生")
print("2. 删除学生")
print("3. 修改学生")
print("4. 查找学生")
print("5. 显示所有学生")
print("0. 退出程序")
choice = input("请输入您的选择:")
if choice == "1":
name = input("请输入学生姓名:")
age = input("请输入学生年龄:")
score = input("请输入学生成绩:")
student = Student(name, age, score)
manager.add_student(student)
print("添加成功!")
elif choice == "2":
name = input("请输入要删除的学生姓名:")
if manager.remove_student(name):
print("删除成功!")
else:
print("该学生不存在!")
elif choice == "3":
name = input("请输入要修改的学生姓名:")
age = input("请输入修改后的学生年龄:")
score = input("请输入修改后的学生成绩:")
if manager.modify_student(name, age, score):
print("修改成功!")
else:
print("该学生不存在!")
elif choice == "4":
name = input("请输入要查找的学生姓名:")
student = manager.search_student(name)
if student:
print("姓名:{},年龄:{},成绩:{}".format(student.name, student.age, student.score))
else:
print("该学生不存在!")
elif choice == "5":
manager.show_all_students()
elif choice == "0":
break
else:
print("输入有误,请重新输入!")
```
这个学生管理系统可以添加、删除、修改、查找和显示所有学生信息。你可以根据自己的需求进行修改和扩展。
阅读全文