python学生信息管理系统(完整版)

时间:2022-10-02 10:09:13

本文是基于上一篇(python项目:学生信息管理系统(初版) )进行了完善,并添加了新的功能。

主要包括有:

完善部分:输入错误;无数据查询等异常错误

新的功能:文件的操作:文件的读写,其中重点是对文本字符串的详细解析(关于整个解析拆解和重组详见代码,以及添加了注释)

学生信息管理系统(完整版)

学生信息管理项目,要求带操作界面,并完成每项操作:

+----------------------+
| 1)添加学生信息             |
| 2)显示所有学生的信息          |
| 3)删除学生信息             |
| 4)修改学生信息             |
| 5)按学生成绩高-低显示学生信息     |
| 6)按学生成绩低-高显示学生信息     |
| 7)按学生年龄高-低显示学生信息     |
| 8)按学生年龄低-高显示学生信息     |
|  9)保存学生信息到文件(students.txt)                |
| 10)从文件中读取数据(students.txt)                |
| 退出:其他任意按键<回车>                                  |
+----------------------+   

详细代码如下:

?
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
# student_info.py
# 学生信息管理项目,要求带操作界面,并完成每项操作:
# +----------------------+
# | 1)添加学生信息                      |
# | 2)显示所有学生的信息                 |
# | 3)删除学生信息                      |
# | 4)修改学生信息                      |
# | 5)按学生成绩高-低显示学生信息         |
# | 6)按学生成绩低-高显示学生信息         |
# | 7)按学生年龄高-低显示学生信息         |
# | 8)按学生年龄低-高显示学生信息         |
# | 9)保存学生信息到文件(students.txt) |
# | 10)从文件中读取数据(students.txt) |
# | 退出:其他任意按键<回车> |
# +----------------------+          |
 
 
def meun():
 menu_info = '''+----------------------+
| 1)添加学生信息 |
| 2)显示所有学生的信息 |
| 3)删除学生信息 |
| 4)修改学生信息 |
| 5)按学生成绩高-低显示学生信息 |
| 6)按学生成绩低-高显示学生信息 |
| 7)按学生年龄高-低显示学生信息 |
| 8)按学生年龄低-高显示学生信息 |
| 9)保存学生信息到文件(students.txt) |
| 10)从文件中读取数据(students.txt) |
| 退出:其他任意按键<回车> |
+----------------------+
'''
 print(menu_info)
 
 
# 以下二个函数用于sorted排序, key的表达式函数
def get_age(*l):
 for x in l:
 return x.get("age")
def get_score(*l):
 for x in l:
 return x.get("score")
 
# 1)添加学生信息
def add_student_info():
 L = []
 while True:
 n = input("请输入名字:")
 if not n: # 名字为空 跳出循环
 break
 try:
 a = int(input("请输入年龄:"))
 s = int(input("请输入成绩:"))
 except:
 print("输入无效,不是整形数值....重新录入信息")
 continue
 info = {"name":n,"age":a,"score":s}
 L.append(info)
 print("学生信息录入完毕!!!")
 return L
 
# 2)显示所有学生的信息
def show_student_info(student_info):
 if not student_info:
 print("无数据信息.....")
 return
 print("名字".center(8),"年龄".center(4),"成绩".center(4))
 for info in student_info:
 print(info.get("name").center(10),str(info.get("age")).center(4),str(info.get("score")).center(4))
 
# 3)删除学生信息
def del_student_info(student_info,del_name = ''):
 if not del_name:
 del_name = input("请输入删除的学生姓名:")
 for info in student_info:
 if del_name == info.get("name"):
 return info
 raise IndexError("学生信息不匹配,没有找到%s" %del_name)
 
# 4)修改学生信息
def mod_student_info(student_info):
 mod_name = input("请输入修改的学生姓名:")
 for info in student_info:
 if mod_name == info.get("name"):
 a = int(input("请输入年龄:"))
 s = int(input("请输入成绩:"))
 info = {"name":mod_name,"age":a,"score":s}
 return info
 raise IndexError("学生信息不匹配,没有找到%s" %mod_name)
 
# 5)按学生成绩高-低显示学生信息
def score_reduce(student_info):
 print("按学生成绩高-低显示")
 mit = sorted(student_info ,key = get_score,reverse = True)
 show_student_info(mit)
 
# 6)按学生成绩低-高显示学生信息
def score_rise(student_info):
 print("按学生成绩低-高显示")
 mit = sorted(student_info ,key = get_score)
 show_student_info(mit)
 
# 7)按学生年龄高-低显示学生信息
def age_reduce(student_info):
 print("按学生年龄高-低显示:")
 mit = sorted(student_info ,key = get_age,reverse = True)
 show_student_info(mit)
 
# 8)按学生年龄低-高显示学生信息
def age_rise(student_info):
 print("按学生年龄低-高显示:")
 mit = sorted(student_info ,key = get_age)
 show_student_info(mit)
 
# 9)保存学生信息到文件(students.txt)
def save_info(student_info):
 try:
 students_txt = open("students.txt","w") # 以写模式打开,并清空文件内容
 except Exception as e:
 students_txt = open("students.txt", "x") # 文件不存在,创建文件并打开
 for info in student_info:
 students_txt.write(str(info)+"\n") # 按行存储,添加换行符
 students_txt.close()
 
# 10)从文件中读取数据(students.txt)
def read_info():
 old_info = []
 try:
 students_txt = open("students.txt")
 except:
 print("暂未保存数据信息") # 打开失败,文件不存在说明没有数据保存
 return
 while True:
 info = students_txt.readline()
 if not info:
 break
 # print(info)
 info = info.rstrip() # 去掉换行符
 # print(info)
 info = info[1:-1] # 去掉{}
 # print(info)
 student_dict = {} # 单个学生字典信息
 for x in info.split(","): # 以,为间隔拆分
 # print(x)
 key_value = [] # 开辟空间,key_value[0]存key,key_value[0]存value
 for k in x.split(":"): # 以:为间隔拆分
 k = k.strip() # 去掉首尾空字符
 # print(k)
 if k[0] == k[-1] and len(k) > 2: # 判断是字符串还是整数
 key_value.append(k[1:-1]) # 去掉 首尾的'
 else:
 key_value.append(int(k))
 # print(key_value)
 student_dict[key_value[0]] = key_value[1] # 学生信息添加
 # print(student_dict)
 old_info.append(student_dict) # 所有学生信息汇总
 students_txt.close()
 return old_info
 
def main():
 student_info = []
 while True:
 # print(student_info)
 meun()
 number = input("请输入选项:")
 if number == '1':
 student_info = add_student_info()
 elif number == '2':
 show_student_info(student_info)
 elif number == '3':
 try:
 student_info.remove(del_student_info(student_info))
 except Exception as e:
 # 学生姓名不匹配
 print(e)
 elif number == '4':
 try:
 student = mod_student_info(student_info)
 except Exception as e:
 # 学生姓名不匹配
 print(e)
 else:
 # 首先按照根据输入信息的名字,从列表中删除该生信息,然后重新添加该学生最新信息
 student_info.remove(del_student_info(student_info,del_name = student.get("name")))
 student_info.append(student)
 elif number == '5':
 score_reduce(student_info)
 elif number == '6':
 score_rise(student_info)
 elif number == '7':
 age_reduce(student_info)
 elif number == '8':
 age_rise(student_info)
 elif number == '9':
 save_info(student_info)
 elif number == '10':
 student_info = read_info()
 else:
 break
 input("回车显示菜单")
 
main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_27297393/article/details/80599592?utm_source=blogxgwz0