python语言写一个学生管理系统 具有添加、删除、修改、查询学生信息及退出系统功能的简易学生管理系统
时间: 2023-07-31 07:07:22 浏览: 388
Python Web开发动手练习项目V1.0 学生管理系统
5星 · 资源好评率100%
好的,下面是一个简单的学生管理系统的代码示例:
```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, student_id):
for student in self.students:
if student.id == student_id:
self.students.remove(student)
print("删除成功!")
return
print("该学生不存在!")
def modify_student(self, student_id, new_name, new_age, new_gender):
for student in self.students:
if student.id == student_id:
student.name = new_name
student.age = new_age
student.gender = new_gender
print("修改成功!")
return
print("该学生不存在!")
def query_student(self, student_id):
for student in self.students:
if student.id == student_id:
print("姓名:{},年龄:{},性别:{}".format(student.name, student.age, student.gender))
return
print("该学生不存在!")
def print_all_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.添加学生;2.删除学生;3.修改学生信息;4.查询学生信息;5.查看所有学生信息;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":
student_id = input("请输入要删除的学生学号:")
sms.delete_student(student_id)
elif choice == "3":
student_id = input("请输入要修改信息的学生学号:")
new_name = input("请输入新的姓名:")
new_age = input("请输入新的年龄:")
new_gender = input("请输入新的性别:")
sms.modify_student(student_id, new_name, new_age, new_gender)
elif choice == "4":
student_id = input("请输入要查询的学生学号:")
sms.query_student(student_id)
elif choice == "5":
sms.print_all_students()
elif choice == "6":
print("谢谢使用,再见!")
break
else:
print("输入有误,请重新输入!")
if __name__ == '__main__':
main()
```
这个学生管理系统使用了类和方法的概念,其中`Student`类表示学生,包含姓名、年龄、性别和学号等属性。`StudentManagementSystem`类表示学生管理系统,包含添加、删除、修改、查询和查看所有学生信息等方法。在`main`函数中,我们通过不断地输入数字来选择要执行的操作。
阅读全文