用Python制作学生管理系统,具有增加、删除、修改、显示所有、保存到文件、退出功能
时间: 2023-09-30 08:04:59 浏览: 84
好的,我可以为你提供一个基本的学生管理系统的Python代码。请注意,这只是提供一个参考,你可以根据自己的需求进行修改和扩展。
```python
import json
# 定义学生类
class Student:
def __init__(self, name, age, gender, score):
self.name = name
self.age = age
self.gender = gender
self.score = score
# 定义学生管理系统类
class StudentManagementSystem:
def __init__(self):
self.students = []
# 添加学生
def add_student(self, student):
self.students.append(student)
# 删除学生
def remove_student(self, name):
for student in self.students:
if student.name == name:
self.students.remove(student)
return True
return False
# 修改学生信息
def update_student(self, name, age, gender, score):
for student in self.students:
if student.name == name:
student.age = age
student.gender = gender
student.score = score
return True
return False
# 显示所有学生信息
def show_all_students(self):
for student in self.students:
print("Name: {}, Age: {}, Gender: {}, Score: {}".format(student.name, student.age, student.gender, student.score))
# 保存学生信息到文件
def save_to_file(self, filename):
data = []
for student in self.students:
data.append({"name": student.name, "age": student.age, "gender": student.gender, "score": student.score})
with open(filename, "w") as f:
json.dump(data, f)
# 从文件中读取学生信息
def load_from_file(self, filename):
with open(filename, "r") as f:
data = json.load(f)
for student_data in data:
student = Student(student_data["name"], student_data["age"], student_data["gender"], student_data["score"])
self.students.append(student)
# 主函数
def main():
# 创建学生管理系统对象
sms = StudentManagementSystem()
# 加载已有学生信息
sms.load_from_file("students.json")
# 循环菜单
while True:
print("1. Add student")
print("2. Remove student")
print("3. Update student")
print("4. Show all students")
print("5. Save to file")
print("6. Quit")
choice = input("Enter your choice: ")
if choice == "1":
name = input("Enter name: ")
age = input("Enter age: ")
gender = input("Enter gender: ")
score = input("Enter score: ")
student = Student(name, age, gender, score)
sms.add_student(student)
print("Student added successfully.")
elif choice == "2":
name = input("Enter name: ")
if sms.remove_student(name):
print("Student removed successfully.")
else:
print("Student not found.")
elif choice == "3":
name = input("Enter name: ")
age = input("Enter age: ")
gender = input("Enter gender: ")
score = input("Enter score: ")
if sms.update_student(name, age, gender, score):
print("Student updated successfully.")
else:
print("Student not found.")
elif choice == "4":
sms.show_all_students()
elif choice == "5":
filename = input("Enter filename: ")
sms.save_to_file(filename)
print("Data saved to file.")
elif choice == "6":
break
else:
print("Invalid choice. Please try again.")
print("Goodbye!")
if __name__ == '__main__':
main()
```
这个代码中,我们定义了两个类:`Student` 和 `StudentManagementSystem`。`Student` 表示学生,其中包含学生的姓名、年龄、性别和分数。`StudentManagementSystem` 表示学生管理系统,其中包含添加、删除、修改、显示所有、保存到文件和退出等功能。
在 `main()` 函数中,我们创建了一个 `StudentManagementSystem` 对象,并加载已有学生信息。然后进入一个循环菜单,根据用户的输入执行相应的操作,包括添加、删除、修改、显示所有、保存到文件和退出。
你可以根据自己的需求进行修改和扩展这个代码。例如,你可以添加一个搜索学生信息的功能,或者将学生信息存储到数据库中。
阅读全文