Python设计一个小型学生管理系统
时间: 2024-05-27 13:06:55 浏览: 104
Python是一种简单易学且功能强大的编程语言,适合用于开发小型学生管理系统。下面是一个简单的Python学生管理系统的设计思路:
1. 定义学生类,包含学生基本信息和成绩信息。
2. 定义管理类,包含添加学生、删除学生、修改学生信息和查询学生信息等操作。
3. 将学生信息存储在文件中。
以下是一个简单的实现示例:
```python
class Student:
def __init__(self, id, name, age, gender, score):
self.id = id
self.name = name
self.age = age
self.gender = gender
self.score = score
class Manager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
break
def modify_student(self, id, name, age, gender, score):
for student in self.students:
if student.id == id:
student.name = name
student.age = age
student.gender = gender
student.score = score
break
def search_student(self, id):
for student in self.students:
if student.id == id:
return student
def show_all(self):
for student in self.students:
print(f'ID: {student.id}, Name: {student.name}, Age: {student.age}, Gender: {student.gender}, Score: {student.score}')
def save_to_file(self):
with open('students.txt', 'w') as f:
for student in self.students:
f.write(f'{student.id},{student.name},{student.age},{student.gender},{student.score}\n')
def load_from_file(self):
try:
with open('students.txt', 'r') as f:
lines = f.readlines()
for line in lines:
data = line.strip().split(',')
student = Student(data, data, data, data, data)
self.add_student(student)
except FileNotFoundError:
pass
manager = Manager()
manager.load_from_file()
while True:
print('1. Add student')
print('2. Remove student')
print('3. Modify student')
print('4. Search student')
print('5. Show all students')
print('6. Save to file')
print('7. Exit')
choice = input('Please enter your choice: ')
if choice == '1':
id = input('Please enter ID: ')
name = input('Please enter name: ')
age = input('Please enter age: ')
gender = input('Please enter gender: ')
score = input('Please enter score: ')
student = Student(id, name, age, gender, score)
manager.add_student(student)
elif choice == '2':
id = input('Please enter ID: ')
manager.remove_student(id)
elif choice == '3':
id = input('Please enter ID: ')
name = input('Please enter new name: ')
age = input('Please enter new age: ')
gender = input('Please enter new gender: ')
score = input('Please enter new score: ')
manager.modify_student(id, name, age, gender, score)
elif choice == '4':
id = input('Please enter ID: ')
student = manager.search_student(id)
if student:
print(f'ID: {student.id}, Name: {student.name}, Age: {student.age}, Gender: {student.gender}, Score: {student.score}')
else:
print('Student not found.')
elif choice == '5':
manager.show_all()
elif choice == '6':
manager.save_to_file()
elif choice == '7':
break
```
阅读全文