Python实现学生信息管理系统

学生信息管理系统是每个语言入门的第一个基础项目吧算是,主要业务逻辑就是增删改查(curd),并将学生信息通过文件进行读取,然后保存,这里使用student_info.txt文本文档进行保存,其实使用数据库保存也是一样的。

为了使项目具有更高的可读性,本项目采用模块化设计,将每种功能(curd功能)通过函数封装起来。

1,打印主菜单menu()

  • 通过menu()函数实现,具体代码如下:
1
2
3
4
5
6
7
8
9
10
def menu():
print('-' * 20)
print('欢迎登录学员管理系统')
print('1: 添加学员')
print('2: 删除学员')
print('3: 修改学员信息')
print('4: 查询学员信息')
print('5: 显示所有学员信息')
print('6: 退出系统')
print('-' * 20)

2,主函数main()

  • 在与用户交互的时候是比较容易出现一些错误的,因此对用户输入的数值进行异常捕获,捕获后提示用户输入错误。并通过if,elif, else基础语法来等价于c语言中的switch,choice。
  • 异常捕获中Exception是所有异常的父类,捕获到异常之后可以print(e)将异常输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def main():
data_load() # 从保存的文件中读取数据
menu()
while True:
try:
choice = int(input('请输入要进行的操作:'))
if choice == 1:
add_student() # 添加学生信息
elif choice == 2:
del_student() # 删除学生信息
elif choice == 3:
update_student_info() # 修改学员信息
elif choice == 4:
find_student_info() # 查询学员信息
elif choice == 5:
show_all() # 显示所有学生信息
elif choice == 6:
data_save() # 保存数据
print("再见!")
break
except Exception as e:
print("请输入1-6之间的数字哦!")

3,添加学生信息add_student()

  • 在这个模块中有个逻辑很容易忽略,就是通过用户输入的学生编号(主键)来判断该学生是否存在,如果存在就不必添加学生了。
  • 在函数中通过global方法达到对全局变量的修改,这也是要注意的一个点。
  • 单条学生信息保存在字典中,所有的学生信息保存在列表中,通过列表的append方法来实现增加学生信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def add_student():
global student_info
new_num = input("请输入学生的学号:")
for i in student_info:
if i['num'] == new_num:
print("此人信息已存在!")
else:
break
new_name = input('请输入学生的名字:')
new_tel = input('请输入学生的电话:')
info_dict = {}
info_dict['name'] = new_name
info_dict['num'] = new_num
info_dict['tel'] = new_tel
student_info.append(info_dict)

4,删除学生del_student()

  • 通过for循环遍历列表,通过比对学生的学号与用户输入的要删除学生学号进行比对。若对上了就将该学生的单条信息进行删除(删除列表中的一个字典)
1
2
3
4
5
6
7
8
9
def del_student():
global student_info
del_num = input("请输入要删除学生的学号:")
for i in student_info:
if i['num'] == del_num:
student_info.remove(i)
print("删除成功!")
else:
continue

5,修改学生信息update_student_info()

  • 跟之前的删除学生的逻辑基本差不多,这里就不做过多赘述了,上代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def update_student_info():
global student_info
update_num = input('请输入要修改学生信息的编号:')
for i in student_info:
try:
if i['num'] == update_num:
new_num = input('请输入新的学生编号:')
new_name = input('请输入新的学生名字:')
new_tel = input('请输入新的学生电话:')
i['name'] = new_name
i['num'] = new_num
i['tel'] = new_tel
print("修改完毕")
else:
continue
except Exception as e:
print(e)

6,查询学生信息find_student_info()

1
2
3
4
5
6
7
8
9
def find_student_info():
global student_info
search_num = input('请输入要查询学生的编号:')
for i in student_info:
if i['num'] == search_num:
print(f'此学生的学号为{i["num"]}, 姓名为{i["name"]}, 电话为{i["tel"]}')
else:
continue

7,显示所有学生信息show_all()

1
2
3
4
5
def show_all():
global student_info
print("姓名\t\t学号\t\t电话\t\t")
for i in student_info:
print(f"{i['name']}\t\t{i['num']}\t\t{i['tel']}")

8,文件写入data_save()

  • 这个地方注意,文件写入到txt文本中、或者其他格式,只有转化成字符串才能写入,同理,读取文件只能读取到字符串。

  • 文件打开方式两种方式f = open(‘student_info.txt’, ‘w’, encoding=’utf8’)

  • 另一种方式 with open(‘student_info.txt’, ‘w’, encoding=’utf8’) as f:f.read()/f.write()

  • 文件操作f.readlines()读取文件的所有行的信息

1
2
3
4
5
def data_save():
global student_info
f = open('student_info.txt', 'w', encoding='utf8')
f.write(str(student_info))
f.close()

9,文件读取data_load()

1
2
3
4
5
6
7
8
9
10
def data_load():
global student_info
f = open('student_info.txt', 'r', encoding='utf8')
file_data = f.read()
if len(file_data) > 0:
# 表明文件中的数据不为空
file_data = eval(file_data) # 将文件中的数据还原
student_info = file_data
f.close()

10,调用函数main

  • 直接写mian然后按tab键就会出现if __name__ == '__main__':
1
2
3
if __name__ == '__main__':
student_info = [] # 定义全局变量一个列表存放学生信息
main() # 调用主函数

需要导入的包(其实都不需要导入,这是我想到的一些数据处理方法,通过json或者通过类对象,但是效果不尽如人意)

1
2
import json
from Exercise.date_define import Record