教你用Python实现简易版学生信息管理系统(含源码)
概述
本文将介绍如何使用 Python 编写一个简单的学生信息管理系统。本系统支持添加、查询、删除和修改学生信息,并且所有数据都存储在本地文本文件中。本文将详细介绍系统的实现流程,并提供完整的源码。
实现步骤
1. 创建项目
首先,在本地环境中创建一个新的 Python 项目文件夹,并在文件夹中创建一个名为 students.txt
的空文本文件。该文件将用于存储所有学生的信息。
2. 实现学生类
创建一个名为 Student
的 Python 类,该类具有以下属性:id
,name
,gender
,age
和 score
。通过实现该类,我们可以方便地对学生信息进行管理。
示例代码:
class Student:
def __init__(self, id, name, gender, age, score):
self.id = id
self.name = name
self.gender = gender
self.age = age
self.score = score
3. 实现数据存储工具类
创建一个名为 DataTool
的 Python 类,该类具有以下方法:
read_data()
:从本地文本文件中读取所有学生的信息,并将其存储在列表中。write_data(data_list)
:将传递的学生信息列表写入本地文本文件。
示例代码:
class DataTool:
def __init__(self, file_path):
self.file_path = file_path
def read_data(self):
with open(self.file_path, 'r') as f:
content = f.readlines()
student_list = []
for item in content:
item = item.strip().split(',')
student_list.append(Student(item[0], item[1], item[2], item[3], item[4]))
return student_list
def write_data(self, data_list):
with open(self.file_path, 'w') as f:
for item in data_list:
write_str = '{0},{1},{2},{3},{4}\n'.format(item.id, item.name, item.gender, item.age, item.score)
f.write(write_str)
4. 实现学生信息管理类
创建一个名为 StudentManager
的 Python 类,该类具有以下方法:
show_all_student()
:打印出所有学生的信息。add_student()
:向学生列表中添加一个新学生。delete_student()
:从学生列表中删除指定学生。update_student()
:更新指定学生的信息。
示例代码:
class StudentManager:
def __init__(self, file_path):
self.data_tool = DataTool(file_path)
self.student_list = self.data_tool.read_data()
def show_all_student(self):
if not self.student_list:
print('No student information available.\n')
return
print('id name gender age score')
for item in self.student_list:
print('{0} {1} {2} {3} {4}'.format(item.id, item.name, item.gender, item.age, item.score))
print()
def add_student(self):
id = input('Please enter the student id: ')
name = input('Please enter the name of the student: ')
gender = input('Please enter the gender of the student: ')
age = input('Please enter the age of the student: ')
score = input('Please enter the score of the student: ')
new_student = Student(id, name, gender, age, score)
self.student_list.append(new_student)
self.data_tool.write_data(self.student_list)
print('Add student successfully!\n')
def delete_student(self):
delete_id = input('Please enter the student id you want to delete: ')
for i in range(len(self.student_list)):
if self.student_list[i].id == delete_id:
del self.student_list[i]
self.data_tool.write_data(self.student_list)
print('Delete student successfully!\n')
return
else:
print('Sorry, student id not found.\n')
def update_student(self):
update_id = input('Please enter the student id you want to update: ')
for item in self.student_list:
if item.id == update_id:
item.name = input('Please enter the new name of the student: ')
item.gender = input('Please enter the new gender of the student: ')
item.age = input('Please enter the new age of the student: ')
item.score = input('Please enter the new score of the student: ')
self.data_tool.write_data(self.student_list)
print('Update student information successfully!\n')
return
else:
print('Sorry, student id not found.\n')
5. 实现入口函数
创建一个名为 main()
的 Python 函数,用于运行该程序。通过调用 StudentManager
类中的方法,可以方便地实现对学生信息的管理。
示例代码:
def main():
file_path = './students.txt'
student_manager = StudentManager(file_path)
while True:
print('=== Student Information Management System ===')
print('1. Show all student information.')
print('2. Add new student.')
print('3. Delete student.')
print('4. Update student information.')
print('0. Quit system.')
choice = input('Please make a choice: ')
if choice == '1':
student_manager.show_all_student()
elif choice == '2':
student_manager.add_student()
elif choice == '3':
student_manager.delete_student()
elif choice == '4':
student_manager.update_student()
elif choice == '0':
print('Thank you for using Student Information Management System, goodbye!')
break
else:
print('Invalid input, please try again.\n')
程序演示
请参考下方两个示例:
示例 1:添加学生信息
=== Student Information Management System ===
1. Show all student information.
2. Add new student.
3. Delete student.
4. Update student information.
0. Quit system.
Please make a choice: 2
Please enter the student id: 001
Please enter the name of the student: John
Please enter the gender of the student: Male
Please enter the age of the student: 18
Please enter the score of the student: 90
Add student successfully!
示例 2:显示所有学生信息
=== Student Information Management System ===
1. Show all student information.
2. Add new student.
3. Delete student.
4. Update student information.
0. Quit system.
Please make a choice: 1
id name gender age score
001 John Male 18 90
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:教你用Python实现简易版学生信息管理系统(含源码) - Python技术站