python设计一个学生管理系统,包括功能:1.添加学生、2.删除学生、3.修改学生信息、4.查询单个学生信息、5.查询所有学生的信息、6.退出系统
时间: 2023-12-16 22:02:24 浏览: 70
好的,下面是一个简单的学生管理系统的 Python 代码实现:
```python
class Student:
def __init__(self, name, age, gender, id_num):
self.name = name
self.age = age
self.gender = gender
self.id_num = id_num
class StudentManagementSystem:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
print("添加成功!")
def delete_student(self, id_num):
for student in self.students:
if student.id_num == id_num:
self.students.remove(student)
print("删除成功!")
return
print("该学生不存在!")
def modify_student(self, id_num):
for student in self.students:
if student.id_num == id_num:
name = input("请输入修改后的姓名:")
age = input("请输入修改后的年龄:")
gender = input("请输入修改后的性别:")
student.name = name if name else student.name
student.age = age if age else student.age
student.gender = gender if gender else student.gender
print("修改成功!")
return
print("该学生不存在!")
def query_student(self, id_num):
for student in self.students:
if student.id_num == id_num:
print(f"姓名:{student.name},年龄:{student.age},性别:{student.gender},学号:{student.id_num}")
return
print("该学生不存在!")
def query_all_students(self):
for student in self.students:
print(f"姓名:{student.name},年龄:{student.age},性别:{student.gender},学号:{student.id_num}")
def exit_system(self):
print("退出系统!")
if __name__ == "__main__":
sms = StudentManagementSystem()
while True:
print("欢迎使用学生管理系统!")
print("1.添加学生\n2.删除学生\n3.修改学生信息\n4.查询单个学生信息\n5.查询所有学生的信息\n6.退出系统")
choice = input("请选择功能:")
if choice == "1":
name = input("请输入姓名:")
age = input("请输入年龄:")
gender = input("请输入性别:")
id_num = input("请输入学号:")
student = Student(name, age, gender, id_num)
sms.add_student(student)
elif choice == "2":
id_num = input("请输入要删除的学生学号:")
sms.delete_student(id_num)
elif choice == "3":
id_num = input("请输入要修改的学生学号:")
sms.modify_student(id_num)
elif choice == "4":
id_num = input("请输入要查询的学生学号:")
sms.query_student(id_num)
elif choice == "5":
sms.query_all_students()
elif choice == "6":
sms.exit_system()
break
else:
print("输入有误,请重新输入!")
```
运行程序后,根据提示选择功能即可。
阅读全文