python实现学生管理系统v1

功能:

展示面板:
1.添加学生的信息
2.删除学生的信息
3.修改学生的信息
4.查询学生的信息
5.遍历所有学生的信息
6.保存并退出系统

需求:

  1. 可以保存用户数据,下次运行程序时能读取上次保存的数据

  2. 具有添加、查询、删除用户的功能

    优化(此版代码未实现):

  3. 规范用户行为,用户输入错误不能直接报错,而是返回相关提示。

  4. 可以增加项目,比如班主任,成绩等。

  5. 可以实现排序功能

  6. 删除一个用户后,索引值自动-1,保证id的连续

  7. 实现批量增加或者删除?

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import csv
from pathlib import Path
import pickle
from prettytable import PrettyTable

p_save = Path('/jupyter/tmp/student/StudentDetails.save') # 可以提取成配置文件
p_csv = Path('/jupyter/tmp/student/StudentDetails.csv') # 可以提取成配置文件
# StudenDetails = [['StudentID', 'name', 'age']]
StudenDetails_Tmp = [['StudentID', 'name', 'age']]


def showinfo():
"""doc"""
print('*' * 40)
'''展示功能面板'''
print(" 1.添加学生的信息")
print(" 2.删除学生的信息")
print(" 3.修改学生的信息")
print(" 4.查询学生的信息")
print(" 5.遍历所有学生的信息")
print(" 6.保存并退出系统")


def save_csv(x): # 保存为csv文件

if not p_csv.parent.exists():
p_csv.parent.mkdir(parents=True) # 相对危险的操作
p_csv.touch()
with open(str(p_csv), 'a+') as f:
writer_student = csv.writer(f)
writer_student.writerows(x)


def save(x):
if not p_save.parent.exists():
p_save.parent.mkdir(parents=True) # 相对危险的操作
p_save.touch()
with open(str(p_save), 'wb+') as f:
pickle.dump(x, f)


def pick_load():
with open(str(p_save), 'rb') as f:
StudenDetails_Tmp = pickle.load(f)
return StudenDetails_Tmp



def generate_counter(lst=[0]): ##闭包实现自增功能==> 学生学号
CNT = lst

def add_one():
CNT[0] += 1
return CNT[0]

return add_one


def show_student_details(lst: list, show_id: int): # 根据输入的id,打印学生的详情
for ii in lst:
for xx in ii:
if xx == show_id: #
details_list = ii

else:
continue
return details_list


def StudentManagement(): # 主体函数

while True: # TODO 完成所有操作最后持久化
print('*' * 40)
option_tmp = input("请选择您的操作:")
while True:
if option_tmp.isdigit():
break
else:
option_tmp = input("请输入正确的操作(1-6):")
option = int(option_tmp)

if option == 1:
StudentID = counter() # TODO 自增问题

name = input("请输入学生的姓名:")
while True:
if (u'\u4e00' <= name <= u'\u9fff'):
break
if name.isalpha():
break
else:
name = input("请输入正确的年龄(中文或者字母):")

age = input("请输入学生的年龄:")
while True:
if age.isdigit():
break
else:
age = input("请输入正确的年龄(1-99):")

StudenDetails_Tmp.extend([[StudentID, name, age]])


elif option == 2: # 输入学生的id就可以删除
DelID = int(input("请输入需要删除的学生id:"))
del_list = show_student_details(StudenDetails_Tmp, DelID)
StudenDetails_Tmp.remove(del_list)
print("删除用户成功:", del_list)


elif option == 3: # 输入学生的id,然后弹出需要修改的选选项,输入值进行修改
ChgDetil = int(input("请输入需要修改的学生id:"))
show_list = show_student_details(StudenDetails_Tmp, ChgDetil)
print("该学生目前的信息:", show_list)
chg_name = input("请输入学生的姓名:")
chg_age = input("请输入学生的年龄:")
new_student_details = [ChgDetil, chg_name, chg_age]
st_index = StudenDetails_Tmp.index(show_list)
StudenDetails_Tmp[st_index] = new_student_details

elif option == 4: # 根据学生id查找学生信息 TODO 可改进为根据任意一个字段查找
ChgDetil = int(input("请输入需要查询的学生id:"))
show_list = show_student_details(StudenDetails_Tmp, ChgDetil)
tb4 = PrettyTable()
tb4.field_names = ['StudentID', 'name', 'age']
tb4.add_row(show_list)

print(tb4)

elif option == 5: # 查询所有的学生信息
tb5 = PrettyTable()
for idx,v in enumerate(StudenDetails_Tmp):
if idx == 0:
tb5.field_names = v
if idx > 0:
tb5.add_row(v)

print(tb5)

elif option == 6:

save_csv(StudenDetails_Tmp) #保存为csv格式便于下载
save(StudenDetails_Tmp) #数据序列化保存
print('保存成功:',StudenDetails_Tmp)
break

else:
print('请输入正确的选项(1-6)')
continue


if __name__ == "__main__":
if p_save.exists():
StudenDetails_Tmp = pick_load()
index_student = [StudenDetails_Tmp[-1][0]]
else:
index_student = [0]
showinfo()
counter = generate_counter(index_student)
StudentManagement()
pick_load()
# print(StudenDetails_Tmp)
****************************************
 1.添加学生的信息
 2.删除学生的信息
 3.修改学生的信息
 4.查询学生的信息
 5.遍历所有学生的信息
 6.保存并退出系统
****************************************
请选择您的操作:1
请输入学生的姓名:zz
请输入学生的年龄:32
****************************************
请选择您的操作:1
请输入学生的姓名:xx
请输入学生的年龄:12
****************************************
请选择您的操作:1
请输入学生的姓名:张三
请输入学生的年龄:12
****************************************
请选择您的操作:1
请输入学生的姓名:44
请输入正确的年龄(中文或者字母):jk
请输入学生的年龄:55
****************************************
请选择您的操作:5
+-----------+------+-----+
| StudentID | name | age |
+-----------+------+-----+
|     1     |  gg  |  44 |
|     2     |  gg  |  55 |
|     3     |  hh  |  66 |
|     4     |  zz  |  32 |
|     5     |  xx  |  12 |
|     6     | 张三 |  12 |
|     7     |  jk  |  55 |
+-----------+------+-----+
****************************************
请选择您的操作:4
请输入需要查询的学生id:2
+-----------+------+-----+
| StudentID | name | age |
+-----------+------+-----+
|     2     |  gg  |  55 |
+-----------+------+-----+
****************************************
请选择您的操作:3
请输入需要修改的学生id:2
该学生目前的信息: [2, 'gg', '55']
请输入学生的姓名:xx
请输入学生的年龄:66
****************************************
请选择您的操作:2
请输入需要删除的学生id:1
删除用户成功: [1, 'gg', '44']
****************************************
请选择您的操作:5
+-----------+------+-----+
| StudentID | name | age |
+-----------+------+-----+
|     2     |  xx  |  66 |
|     3     |  hh  |  66 |
|     4     |  zz  |  32 |
|     5     |  xx  |  12 |
|     6     | 张三 |  12 |
|     7     |  jk  |  55 |
+-----------+------+-----+
****************************************
请选择您的操作:6
保存成功: [['StudentID', 'name', 'age'], [2, 'xx', '66'], [3, 'hh', '66'], [4, 'zz', '32'], [5, 'xx', '12'], [6, '张三', '12'], [7, 'jk', '55']]
---------------- 谢谢光临 ----------------

本文标题:python实现学生管理系统v1

文章作者:pxrux

发布时间:2019年04月23日 - 14:04

最后更新:2019年08月13日 - 09:08

原始链接:http://www.mykernel.cn/my-Project-student-manger.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%