这个是升级的版本,比较进阶一点的,相对与之前的文件管理系统,数据只是存储到了内存中,而不是存储到硬盘上,我们想让文件存储到硬盘上的话,一个是存储到文件里,一个是存储到数据库中,存储到数据库中的版本会后续发布,先来看一下文件存储版,是把学生信息存储到了txt文件中,我这里是默认存储到了students.txt文件中,想更改文件名字根据代码修改即可,代码中注释很详细,我也是python初学者,欢迎大家和我交流!
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
"""
学生管理系统(文件存储版)
再原有功能的基础上添加了如下功能:
1.保存学生数据,保存到与python文件同目录下 保存的时候设置编码utf-8 防止乱码
2.删除所有学生信息
3.添加了函数recover_data运行程序先获取数据 文件不存在的话会自行创建并存储一个空列表
"""
import os
name_list = [] # 存储学生信息字典,学生信息用字典存,再用列表存储字典
# 菜单
def display_menu():
print ( "-" * 30 )
print ( " 学生管理系统 v8.8 " )
print ( "1.添加学生信息" )
print ( "2.删除学生信息" )
print ( "3.修改学生信息" )
print ( "4.查询单个学生信息" )
print ( "5.查询所有学生信息" )
print ( "6.保存学生信息" )
print ( "7.删除所有学生" )
print ( "0.退出系统" )
print ( "-" * 30 )
# 选择序号的获得
def get_choice():
selected_key = input ( "请输入选择的序号:" )
return selected_key
# 检查性别是否合法
def check_sex(new_sex):
flag = true
while flag:
if new_sex = = '男' or new_sex = = '女' :
flag = false
else :
new_sex = input ( "输入性别有误,请重新输入(男/女):" )
return new_sex
# 检查电话号码是否合法
def check_phone(new_phone):
flag = true
while flag:
if new_phone.isdigit():
flag = false
else :
new_phone = input ( "您输入的电话号码有误,请重新输入:" )
return new_phone
# 检查学号是否重复或者有误
def check_id(new_id):
flag = true
while flag:
# 先检查是不是纯数字再去考虑是否重复的事情,如果不是纯数字直接pass
if new_id.isdigit():
for i in range ( len (name_list)):
if name_list[i][ 'id' ] = = new_id:
new_id = check_id( input ( "您输入的学号重复,请重新输入:" ))
flag = false
else :
new_id = input ( "您输入的学号有误,请重新输入:" )
return new_id
# 添加学生信息
def add_name():
new_info = {}
new_id = check_id( input ( "请输入学号:" ))
new_info[ 'id' ] = new_id
new_name = input ( "请输入姓名:" )
new_info[ 'name' ] = new_name
new_sex = check_sex( input ( "请输入性别(男/女):" ))
new_info[ 'sex' ] = new_sex
new_phone = check_phone( input ( "请输入电话号码:" ))
new_info[ 'phone' ] = new_phone
name_list.append(new_info)
print ( "添加成功!" )
# 查询所有学生信息
def find_all():
print ( "=" * 30 )
for name in name_list:
print (name[ 'id' ], name[ 'name' ], name[ 'sex' ], name[ 'phone' ])
print ( "=" * 30 )
# 删除学生信息
def del_name():
del_id_is = input ( "请输入要删除的学生学号:" )
flag = false
index = 0
for i in range ( len (name_list)):
if name_list[i][ 'id' ] = = del_id_is:
flag = true
index = i
break
if flag:
name_list.pop(index)
print ( "删除成功!" )
else :
print ( "学生未找到!请检查学号输入是否有误!" )
# 删除所有学生
def del_all():
global name_list
flag = input ( "确定要删除吗?(y删除/输入其他字符返回菜单):" )
if flag = = 'y' or flag = = 'y' :
name_list = []
print ( "删除成功!" )
else :
return
# 名字修改细节函数
def choice_of_name(index):
while true:
choice = input ( "请输入要修改学生的(1.id 2.姓名 3.性别 4.电话号码,5.全部修改):" )
if choice = = '5' :
new_id = input ( "请输入新的学号:" )
name_list[index][ 'id' ] = new_id
new_name = input ( "请输入新的姓名:" )
name_list[index][ 'name' ] = new_name
new_sex = check_sex( input ( "请输入性别(男/女):" ))
name_list[index][ 'sex' ] = new_sex
new_phone = check_phone( input ( "请输入电话号码:" ))
name_list[index][ 'phone' ] = new_phone
break
elif choice = = '1' :
new_id = input ( "请输入新的学号:" )
name_list[index][ 'id' ] = new_id
break
elif choice = = '2' :
new_name = input ( "请输入新的姓名:" )
name_list[index][ 'name' ] = new_name
break
elif choice = = '3' :
new_sex = check_sex( input ( "请输入性别(男/女):" ))
name_list[index][ 'sex' ] = new_sex
break
elif choice = = '4' :
new_phone = check_phone( input ( "请输入电话号码:" ))
name_list[index][ 'phone' ] = new_phone
break
else :
print ( "输入有误,请重新输入!" )
# 修改学生信息
def re_name():
id_is = input ( "请输入要修改的学生学号:" )
flag = false
index = 0
# 先找到要修改的学生的下标
for i in range ( len (name_list)):
if name_list[i][ 'id' ] = = id_is:
flag = true
index = i
break
if flag:
choice_of_name(index)
print ( "修改成功!" )
else :
print ( "修改失败,学生信息未找到!" )
# 查询单个学生信息
def find_name():
find_id_is = input ( "请输入要查询的学生学号:" )
flag = false
index = 0
for i in range ( len (name_list)):
if name_list[i][ 'id' ] = = find_id_is:
flag = true
index = i
break
if flag:
print ( "学生查询到,学生信息为:" )
print (name_list[index][ 'id' ], name_list[index][ 'name' ], name_list[index][ 'sex' ], name_list[index][ 'phone' ])
else :
print ( "学生未找到!" )
# 保存学生信息
def save_stu():
students = str (name_list)
with open ( "students.txt" , "w" , encoding = "utf-8" ) as f:
f.write(students)
print ( "保存成功!文件位置在" + os.getcwd())
# 恢复数据
def recover_data():
global name_list
try :
with open ( "students.txt" , "r" , encoding = "utf-8" ) as f:
content = f.read()
if content ! = '':
name_list = eval (content)
except :
f = open ( "students.txt" , "w" )
f.write( "[]" )
def main():
recover_data()
exit_name = true
while exit_name:
display_menu()
key = get_choice()
if key = = '1' :
add_name()
elif key = = '2' :
del_name()
elif key = = '3' :
re_name()
elif key = = '4' :
find_name()
elif key = = '5' :
find_all()
elif key = = '6' :
save_stu()
elif key = = '7' :
del_all()
elif key = = '0' :
exit_name = false
else :
print ( "请输入正确的数值!" )
main()
|
以上所述是小编给大家介绍的python实现基本的学生管理系统详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.csdn.net/hsunnyc/article/details/89448993