用Python写一个学生管理系统,输入数字1显示学生名单,如果不存在,提示学生名单为空,数字2查询学生信息,不存在提示是否新建学生信息,数字3插入新学生信息,如果存在,提示是否更新学生信息,如果不存在,提示输入学号,并在字典中插入学生信息,数字4进行删除操作,数字0退出程序
时间: 2024-11-26 22:22:31 浏览: 23
在Python中,你可以创建一个简单的命令行界面来实现这样的学生管理系统。这里是一个基础的示例:
```python
class Student:
def __init__(self, id, name):
self.id = id
self.name = name
class StudentManager:
def __init__(self):
self.students = {}
# 显示学生列表
def display_students(self):
if not self.students:
print("学生名单为空")
else:
for id, student in self.students.items():
print(f"ID: {id}, Name: {student.name}")
# 查询学生信息
def search_student(self, id):
if id in self.students:
print(f"学生信息: ID: {id}, Name: {self.students[id].name}")
else:
input("学生不存在,是否要新建? (y/n): ")
# 插入新学生
def add_student(self, id, name):
if id not in self.students:
self.students[id] = Student(id, name)
print("学生信息已添加")
else:
choice = input("学号已存在,是否要更新? (y/n): ")
if choice.lower() == 'y':
self.students[id] = Student(id, name)
# 删除学生
def remove_student(self, id):
if id in self.students:
del self.students[id]
print(f"学生ID: {id} 已从系统中移除")
else:
print("该学生不存在")
# 主函数处理用户选择
def main(self):
while True:
choice = int(input("请输入选项(1-查看学生,2-查询信息,3-添加学生,4-删除学生,0-退出) : "))
if choice == 0:
break
elif choice == 1:
self.display_students()
elif choice == 2:
id = int(input("请输入学生的ID: "))
self.search_student(id)
elif choice == 3:
id = int(input("请输入学生ID: "))
name = input("请输入学生姓名: ")
self.add_student(id, name)
elif choice == 4:
id = int(input("请输入要删除的学生ID: "))
self.remove_student(id)
else:
print("无效的选择,请重新输入")
# 创建并运行学生管理器
manager = StudentManager()
manager.main()
```
在这个例子中,我们首先定义了一个`Student`类表示学生,并在`StudentManager`类中实现了各种操作方法。主函数`main`负责处理用户的输入,引导他们通过数字选择相应功能。
阅读全文
相关推荐

















