1 # ------------功能菜单------------ 2 # 1、录入学生信息 3 # 2、查找学生信息 4 # 3、删除学生信息 5 # 4、修改学生信息 6 # 5、排序 7 # 6、统计学生总人数 8 # 7、显示所有学生信息 9 # 0、退出 10 # -------------------------------- 11 12 import os 13 # student.txt文件创建在代码同文件下 14 fileName = 'student.txt' 15 16 def main(): 17 while True: 18 nav() 19 try: 20 choose = int(input('请选择:')) 21 if choose in [0, 1, 2, 3, 4, 5, 6, 7]: 22 if choose == 0: 23 answer = input('你确定要退出系统吗?请输入y/n或 Y/N') 24 if answer =='y' or answer =='Y': 25 print('谢谢你的使用,退出系统成功') # 退出系统成功 26 break 27 else: 28 continue 29 elif choose == 1: 30 insert() 31 elif choose == 2: 32 search() 33 elif choose == 3: 34 delete() 35 elif choose == 4: 36 modify() 37 elif choose == 5: 38 sort() 39 elif choose == 6: 40 total() 41 elif choose == 7: 42 show() 43 else: 44 print('请输入 0, 1, 2, 3, 4, 5, 6, 7') 45 46 except ValueError as e: 47 print('输入有误!', e) 48 49 def nav(): 50 print('==========学生管理系统==========') 51 print('------------功能菜单------------') 52 print(' 1、录入学生信息') 53 print(' 2、查找学生信息') 54 print(' 3、删除学生信息') 55 print(' 4、修改学生信息') 56 print(' 5、排序') 57 print(' 6、统计学生总人数') 58 print(' 7、显示所有学生信息') 59 print(' 0、退出') 60 print('--------------------------------') 61 62 def insert(): 63 student_list = [] 64 while True: 65 id = input('请输入ID(如10001):') 66 if not id: 67 break 68 name = input('请输入姓名:') 69 if not name: 70 break 71 try: 72 english = int(input('请输入英语成绩:')) 73 python = int(input('请输入Python成绩:')) 74 math = int(input('请输入数学成绩:')) 75 java = int(input('请输入java成绩:')) 76 javascript = int(input('请输入javascript成绩:')) 77 except: 78 print('输入无效。请输入正确的整数类型,请重新输入') 79 continue 80 # 将录入的学生信息保存到字典中 81 student = {'id': id, 'name': name, 'english': english, 'python': python, 'math': math, 'java': java, 'javascript': javascript} 82 # 将学生添加到列表当中 83 student_list.append(student) 84 answer = input('是否确认继续添加?y/n ') 85 if answer == 'y' or answer == 'Y': 86 continue 87 else: 88 break 89 # 调用save()函数 90 save(student_list) 91 print('学生录入完毕!!!') 92 93 def save(lst): 94 try: 95 stu_txt = open(fileName, 'a', encoding='utf-8') 96 except: 97 stu_txt = open(fileName, 'w', encoding='utf-8') 98 for item in lst: 99 a = stu_txt.write(str(item)+' ') 100 print(a) 101 stu_txt.close() 102 103 def search(): 104 student_query = [] 105 while True: 106 id = '' 107 name = '' 108 if os.path.exists(fileName): 109 mode = input('按ID查找请输入1,按姓名查找请输入2:') 110 if mode == '1': 111 id = input('请输入学生ID:') 112 elif mode == '2': 113 name = input('请输入学生姓名:') 114 else: 115 print('你输入有误,请重新输入') 116 search() 117 with open(fileName, 'r', encoding='utf-8') as rfile: 118 student = rfile.readlines() 119 for item in student: 120 d = dict(eval(item)) 121 if id != '': 122 if d['id'] == id: 123 student_query.append(d) 124 elif name != '': 125 if d['name'] == name: 126 student_query.append(d) 127 # 显示查询结果 128 show_student(student_query) 129 # 清空列表 130 student_query.clear() 131 anwser = input('是否要继续查询?y/n') 132 if anwser == 'y' or anwser == 'Y': 133 continue 134 else: 135 break 136 else: 137 print('未查询到学生信息!') 138 return 139 140 def show_student(lst): 141 if len(lst) == 0 : 142 print('没有查询到学生信息,无数据显示!!!') 143 return 144 # 定义标题显示格式 145 format_title = '{:^6} {:^12} {:^12} {:^12} {:^12} {:^12} {:^12} {:^12}' 146 print(format_title.format('ID', '姓名', '英语成绩', 'Python成绩', '数学成绩', 'java成绩', 'javascript成绩', '总成绩')) 147 # 定义显示内 148 format_data = '{:^6} {:^12} {:^12} {:^12} {:^12} {:^12} {:^12} {:^12}' 149 for item in lst: 150 print( 151 format_data.format( 152 item.get('id'), 153 item.get('name'), 154 item.get('english'), 155 item.get('python'), 156 item.get('math'), 157 item.get('java'), 158 item.get('javascript'), 159 int(item.get('english')) + int(item.get('python')) + int(item.get('math')) + int(item.get('java')) + int(item.get('javascript')) 160 ) 161 ) 162 163 def delete(): 164 while True: 165 student_id = input('请输入要删除学生的ID:') 166 if student_id != '': 167 if os.path.exists(fileName): 168 with open(fileName, 'r', encoding='utf-8') as file: 169 student_old = file.readlines() 170 else: 171 student_old = [] 172 flag = False # 标记是否删除 173 if student_old: 174 with open(fileName, 'w', encoding='utf-8') as wfile: 175 d = {} 176 for item in student_old: 177 d = dict(eval(item)) # 将字符转换为字典 178 if d['id'] != student_id: 179 wfile.write(str(d) + ' ') 180 else: 181 flag = True 182 if flag: 183 print('id为',student_id,'的学生信息已经被删除') 184 else: 185 print('没有找到ID为',student_id,'的学生信息') 186 else: 187 print('无学生信息') 188 break 189 show() # 删除之后要重新显示所有学生信息 190 answer = input('是否确认继续添加?y/n ') 191 if answer == 'y' or answer =='Y': 192 continue 193 else: 194 break 195 else: 196 print('请输入数据') 197 break 198 199 def modify(): 200 show() 201 if os.path.exists(fileName): 202 with open(fileName, 'r', encoding='utf-8') as rfile: 203 student_old = rfile.readlines() 204 else: 205 return 206 student_id = input('请输入要修改学员的ID:') 207 with open(fileName, 'w', encoding='utf-8') as wfile: 208 for item in student_old: 209 d = dict(eval(item)) 210 try: 211 212 if d['id'] == student_id: 213 print('找到学生信息,可以修改他的相关信息了!') 214 while True: 215 try: 216 d['name'] = input('请输入姓名:') 217 d['english'] = input('请输入英语成绩:') 218 d['python'] = input('请输入Python成绩:') 219 d['math'] = input('请输入数学成绩:') 220 d['java'] = input('请输入java成绩:') 221 d['javascript'] = input('请输入javascript成绩:') 222 except: 223 print('输入的数据有误,请重新输入!') 224 else: 225 break 226 wfile.write(str(d)+' ') 227 print('修改成功!') 228 else: 229 wfile.write(str(d)+' ') 230 print('未查询到你输入的ID值!') 231 except: 232 print('输入的数据有误,请重新输入!') 233 234 answer = input('是否继续修改其他学生信息?y/n或者Y/N') 235 if answer == 'y' or answer == 'Y': 236 modify() 237 238 def sort(): 239 show() 240 if os.path.exists(fileName): 241 with open(fileName, 'r', encoding='utf-8') as rfile: 242 student_list = rfile.readlines() 243 student_new = [] 244 for item in student_list: 245 d = dict(eval(item)) 246 student_new.append(d) 247 else: 248 return 249 250 asc_or_desc = input('请选择(0.升序,1.降序):') 251 if asc_or_desc == '0': 252 asc_or_desc_bool = False 253 elif asc_or_desc == '1': 254 asc_or_desc_bool = True 255 else: 256 print('你的输入有误,请重新输入') 257 sort() 258 mode = input( 259 '请选择排序方式(1.按English排序 2.按python排序 3.按math排序 4.按java排序 5.按javaScript排序 6.按总成绩排序)' 260 ) 261 if mode == '1': 262 student_new.sort(key=lambda x: int(x['english']), reverse=asc_or_desc_bool) 263 elif mode == '2': 264 student_new.sort(key=lambda x: int(x['python']), reverse=asc_or_desc_bool) 265 elif mode == '3': 266 student_new.sort(key=lambda x: int(x['math']), reverse=asc_or_desc_bool) 267 elif mode == '4': 268 student_new.sort(key=lambda x: int(x['java']), reverse=asc_or_desc_bool) 269 elif mode == '5': 270 student_new.sort(key=lambda x: int(x['javascript']), reverse=asc_or_desc_bool) 271 elif mode == '6': 272 student_new.sort(key=lambda x: int(x['english']) + int(x['python']) + int(x['math']) + int(x['java']) + int(x['javascript']), reverse=asc_or_desc_bool) 273 else: 274 print('输入的排序方式有误!') 275 sort() 276 show_student(student_new) 277 278 279 def total(): 280 if os.path.exists(fileName): 281 with open(fileName, 'r', encoding='utf-8') as rfile: 282 students = rfile.readlines() 283 if students: 284 print('共有',len(students),'名学生') 285 else: 286 print('还没有录入学生信息') 287 else: 288 print('暂未保存数据信息......') 289 290 291 292 def show(): 293 student_list = [] 294 if os.path.exists(fileName): 295 with open(fileName, 'r', encoding='utf-8') as rfile: 296 students = rfile.readlines() 297 for item in students: 298 student_list.append(eval(item)) 299 if student_list: 300 show_student(student_list) 301 302 else: 303 print('暂未保存数据......') 304 305 if __name__ == '__main__': 306 main() 307 308 # 打包 pyinstaller -F E:workspacePython 基础学python练习stystudentsstusystem.py
目录结构
运行截图