用python写学生信息管理系统
时间: 2023-10-01 21:10:27 浏览: 63
好的,下面是一个简单的学生信息管理系统的 Python 代码:
```python
class Student:
def __init__(self, name, age, gender, id):
self.name = name
self.age = age
self.gender = gender
self.id = id
class StudentManagementSystem:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
print("添加成功!")
def delete_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
print("删除成功!")
return
print("未找到该学生!")
def search_student(self, id):
for student in self.students:
if student.id == id:
print("姓名:{},年龄:{},性别:{},学号:{}".format(student.name, student.age, student.gender, student.id))
return
print("未找到该学生!")
def modify_student(self, id, name=None, age=None, gender=None):
for student in self.students:
if student.id == id:
if name:
student.name = name
if age:
student.age = age
if gender:
student.gender = gender
print("修改成功!")
return
print("未找到该学生!")
def show_students(self):
print("所有学生信息:")
for student in self.students:
print("姓名:{},年龄:{},性别:{},学号:{}".format(student.name, student.age, student.gender, student.id))
def main():
sms = StudentManagementSystem()
while True:
print("1. 添加学生")
print("2. 删除学生")
print("3. 查找学生")
print("4. 修改学生信息")
print("5. 显示所有学生信息")
print("6. 退出系统")
choice = input("请输入您的选择:")
if choice == "1":
name = input("请输入学生姓名:")
age = input("请输入学生年龄:")
gender = input("请输入学生性别:")
id = input("请输入学生学号:")
student = Student(name, age, gender, id)
sms.add_student(student)
elif choice == "2":
id = input("请输入要删除的学生学号:")
sms.delete_student(id)
elif choice == "3":
id = input("请输入要查找的学生学号:")
sms.search_student(id)
elif choice == "4":
id = input("请输入要修改信息的学生学号:")
name = input("请输入修改后的学生姓名(不修改请按回车):")
age = input("请输入修改后的学生年龄(不修改请按回车):")
gender = input("请输入修改后的学生性别(不修改请按回车):")
sms.modify_student(id, name, age, gender)
elif choice == "5":
sms.show_students()
elif choice == "6":
print("感谢使用!")
break
else:
print("输入有误,请重新输入!")
if __name__ == "__main__":
main()
```
这个学生信息管理系统包括了添加、删除、查找、修改和显示所有学生信息的功能。你可以根据自己的需求进行修改和扩展。
阅读全文