项目-学生管理系统

项目-学生管理系统

目标

综合应用已经学习过的知识点:

  • 变量
  • 流程控制
  • 函数
  • 文件操作
  • 列表
  • 字典
  • 字符串

系统需求

  1. 程序启动,显示功能菜单

    功能菜单

  2. 用户根据不同数字选择不同功能

  3. 学生记录的信息有姓名,性别和电话

方法一:使用全局变量保存信息

主函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def main():
show_menu()
while True:
value = input("请输入你的选择:")
value = int(value)
# 新增学生记录
if value == 1:
add_student()

# 查询学生记录
elif value == 2:
find_name = input("请输入要查询的学生姓名:")
print(stu_find(find_name))

# 删除学生记录
elif value == 3:
del_stu_name = input("请输入要删除学生的姓名:")
stu_del(del_stu_name)

# 显示所有学生记录
elif value == 4:
for x in STUDENT:
print(x)

# 退出系统
elif value == 0:
return
else:
print("请输入正确的选项!")
continue

if __name__ == '__main__':
main()

功能菜单显示函数

1
2
3
4
5
6
def show_menu():
print("1. 新增学生记录")
print("2. 查询学生记录")
print("3. 删除学生记录")
print("4. 显示全部学生")
print("0. 退出管理系统")

新增学生记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def add_student():
stu_name = input("请输入学生姓名:")
stu_sex = input("请输入学生性别:")
stu_phone = input("请输入学生电话:")

# 定义一个字典用来存学生信息
student = {}
student['name'] = stu_name
student['sex'] = stu_sex
student['phone'] = stu_phone

# 将每个学生的信息字典加入到总列表中
STUDENT.append(student)
print("新增学生成功")

查询学生信息

1
2
3
4
5
6
7
8
9
10
11
def stu_find(find_stu_name):
is_value = False
# 遍历列表
for x in STUDENT:
# 如果找到该学生,返回列表中的成员字典
if find_stu_name == x['name']:
is_value = True
return x
else:
is_value = False
return -1

删除学生信息

1
2
3
4
5
6
def stu_del(del_stu_name):
is_value = stu_find(del_stu_name)
if is_value == -1:
print("数据库中不存在此学生记录")
else:
STUDENT.remove(is_value)

程序运行结果

程序运行结果

方法二:使用文本保存学生记录改写

主函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def main():
show_menu()
while True:
value = input("请输入你的选择:")
value = int(value)
if value == 1:
add_student()
elif value == 2:
find_name = input("请输入要查询的学生姓名:")
stu_find(find_name)
elif value == 3:
del_stu_name = input("请输入要删除学生的姓名:")
stu_del(del_stu_name)
elif value == 4:
show_all()
elif value == 0:
return
else:
print("请输入正确的选项!")
continue

if __name__ == '__main__':
main()

新增学生记录

1
2
3
4
5
6
7
8
9
def add_student():
stu_name = input("请输入学生姓名:")
stu_sex = input("请输入学生性别:")
stu_phone = input("请输入学生电话:")
# 以“|”来分隔
line = "姓名:{0}|性别:{1}|电话:{2}\n".format(stu_name,stu_sex,stu_phone)
with open("student_information.txt", "a") as fp:
fp.writelines(line)
print("新增学生成功")

查询学生记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def stu_find(find_stu_name):
num = 0
with open("student_information.txt", "r") as fp:
for line in fp:
# 去除每行最后的一个字符“\n”,然后以“|”分隔字符串
infos = line.replace("\n", "").split("|")
# 然后以“:”将上面分隔完成的继续以“:”分隔成列表
student_list = [x.split(':',1) for x in infos]
# 将列表转化成字典
student_dict = dict(student_list)
#print(student_dict)
if student_dict.get('姓名') == find_stu_name:
num += 1
print(student_dict)
if num == 0:
print("数据库中没有此人")

删除学生信息

1
2
3
4
5
6
7
8
9
10
11
def stu_del(del_stu_name):
temp = ''
with open("student_information.txt", "r+") as fp:
for line in fp:
if del_stu_name in line:
continue
else:
temp = line
fp.close()
with open("student_information.txt", "w+") as fp:
fp.write(temp)

这里的逻辑就是去一行一行的读,如果那行存在要删除学生的姓名的字符串 就不把那行缓存到temp变量中,等读完再写回源文件来进行删除操作

方法三:使用类以及封装的思想

学生类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Student(object):
def __init__(self, student_name, student_sex, student_phone):
self.name = student_name
self.sex = student_sex
self.phone = student_phone

@classmethod
def line_to_stu(cls,line):
if line == None:
return None
else:
student_name, student_sex,student_phone = line.replace('\n','').split('|')
student = Student(student_name,student_sex,student_phone)
return student

def stu_to_line(self):
return "{name}|{sex}|{phone}\n".format(name = self.name,sex = self.sex,phone = self.phone)

学生的管理类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class StuManager(object):
__instance = None
__filename = 'student_information.txt'

def __new__(cls, *args, **kwargs):
if not cls.__instance:
cls.__instance = super(StuManager,cls).__new__(cls, *args, **kwargs)
return cls.__instance

def add_student(self,student_name, student_sex, student_phone):
student = Student(student_name,student_sex,student_phone)
with open(StuManager.__filename,'a') as fp:
fp.write(student.stu_to_line())
fp.close()

def search_student(self,student_name):
with open(StuManager.__filename, 'r') as fp:
for line in fp:
student = Student.line_to_stu(line)
if student.name == student_name:
return student
fp.close()
return None

def delete_student(self,student_name):
temp = ''
num = 0
with open(StuManager.__filename, 'r') as fp:
for line in fp.readlines():
if student_name in line:
num += 1
continue
else:
temp += line
fp.close()
if num == 0:
print("无此人的记录")
return
with open(StuManager.__filename,'w') as fp:
fp.write(temp)
fp.close()

def list_all_student(self):
all_student = []
with open(StuManager.__filename, 'r') as fp:
for line in fp:
student = Student.line_to_stu(line)
if student == None:
print("一个学生的记录都没有")
return
else:
all_student.append(student)
fp.close()
return all_student

用来执行的流程类

主要就是对学生的管理类进行再次封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Application(object):
__instance = None
def __new__(cls, *args, **kwargs):
if not cls.__instance:
cls.__instance = super(Application, cls).__new__(cls, *args, **kwargs)
return cls.__instance

def __add_new_student(self):
stu_name = input("请输入学生姓名:")
stu_sex = input("请输入学生性别:")
stu_phone = input("请输入学生电话:")
student_manager.add_student(stu_name,stu_sex,stu_phone)
print("新增学生成功")

def __find_student(self):
student_name = input("请输入想要查询的学生姓名:")
student = student_manager.search_student(student_name)
if student:
print("name\t\tsex\t\tphone")
print("{0}\t\t{1}\t\t{2}".format(student.name, student.sex, student.phone))
else:
print("没有此学生的记录")

def __del_student(self):
student_name = input("请输入想要删除的学生姓名:")
student = student_manager.delete_student(student_name)

def __list_all_student(self):
all_student = student_manager.list_all_student()
print("name\t\tsex\t\tphone")
for student in all_student:
print("{0}\t\t{1}\t\t{2}".format(student.name,student.sex,student.phone))

def run(self):
while True:
print("1. 新增学生记录")
print("2. 查询学生记录")
print("3. 删除学生记录")
print("4. 显示全部学生")
print("0. 退出管理系统")
option = input("请输入操作序号:")
if option == '0':
return
elif option == '1':
app.__add_new_student()
elif option == '2':
app.__find_student()
elif option == '3':
app.__del_student()
elif option == '4':
app.__list_all_student()

主函数

1
2
3
4
5
6
7
student_manager = StuManager()
app = Application()
def main():
app.run()

if __name__ == '__main__':
main()

小结

本篇以三种方法实现了学生管理系统,待后续学了数据库的接口在进行优化。本篇有些函数只是为了夯实基础故意这么设计,其实不必这么麻烦。