设计一个学生管理系统,包括功能:1.添加学生《需要包含如学号,姓名性别,籍贯等信息)、2.删除学生、3.修改学生信息、4.查询单个学生信息5.查询所有学生的信息、6.退出系统。“
时间: 2024-02-27 07:56:10 浏览: 59
以下是一个简单的学生管理系统的 Python 实现代码,包含添加学生、删除学生、修改学生信息、查询单个学生信息、查询所有学生信息和退出系统等功能:
```python
# 定义学生信息类
class Student:
def __init__(self, id, name, gender, hometown):
self.id = id
self.name = name
self.gender = gender
self.hometown = hometown
# 定义学生管理系统类
class StudentManagementSystem:
def __init__(self):
self.students = []
# 添加学生
def add_student(self, id, name, gender, hometown):
student = Student(id, name, gender, hometown)
self.students.append(student)
# 删除学生
def delete_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
print(f"学号为{id}的学生已删除")
return
print(f"学号为{id}的学生不存在")
# 修改学生信息
def update_student(self, id, name, gender, hometown):
for student in self.students:
if student.id == id:
student.name = name
student.gender = gender
student.hometown = hometown
print(f"学号为{id}的学生已更新")
return
print(f"学号为{id}的学生不存在")
# 查询单个学生信息
def query_student(self, id):
for student in self.students:
if student.id == id:
print(f"学号:{student.id} 姓名:{student.name} 性别:{student.gender} 籍贯:{student.hometown}")
return
print(f"学号为{id}的学生不存在")
# 查询所有学生信息
def query_all_students(self):
if len(self.students) == 0:
print("暂无学生信息")
else:
for student in self.students:
print(f"学号:{student.id} 姓名:{student.name} 性别:{student.gender} 籍贯:{student.hometown}")
# 创建学生管理系统对象
sms = StudentManagementSystem()
# 学生管理系统菜单
while True:
print("欢迎使用学生管理系统")
print("1.添加学生 2.删除学生 3.修改学生信息 4.查询单个学生信息 5.查询所有学生信息 6.退出系统")
choice = input("请输入操作序号:")
# 添加学生
if choice == "1":
id = input("请输入学生学号:")
name = input("请输入学生姓名:")
gender = input("请输入学生性别:")
hometown = input("请输入学生籍贯:")
sms.add_student(id, name, gender, hometown)
# 删除学生
elif choice == "2":
id = input("请输入要删除的学生学号:")
sms.delete_student(id)
# 修改学生信息
elif choice == "3":
id = input("请输入要修改的学生学号:")
name = input("请输入修改后的学生姓名:")
gender = input("请输入修改后的学生性别:")
hometown = input("请输入修改后的学生籍贯:")
sms.update_student(id, name, gender, hometown)
# 查询单个学生信息
elif choice == "4":
id = input("请输入要查询的学生学号:")
sms.query_student(id)
# 查询所有学生信息
elif choice == "5":
sms.query_all_students()
# 退出系统
elif choice == "6":
print("感谢使用学生管理系统,再见!")
break
else:
print("输入有误,请重新输入")
```
阅读全文
相关推荐
















