一个学生信息管理系统。功能挺多的,界面如下:
[Python] 纯文本查看 复制代码
?
| 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | import pickle from os.path import isfile from os import remove def main(): running = True while running: menu() code = '9' count = 0 while code not in [str(i) for i in range(8)]: if count == 0: code = input("请输入:") else: code = input("输入有误!请重新输入:") count += 1 if code == '0': running = False elif code == '1': insert() elif code == '2': search() elif code == '3': delete() elif code == '4': modify() elif code == '5': sort() elif code == '6': total() elif code == '7': show() else: input("感谢您使用学生信息查询系统程序,请按 def menu(): print(''' ╔———————学生信息管理系统————————╗ │ │ │ =============== 功能菜单 =============== │ │ │ │ 1 录入学生信息 │ │ 2 查找学生信息 │ │ 3 删除学生信息 │ │ 4 修改学生信息 │ │ 5 排序 │ │ 6 统计学生总人数 │ │ 7 显示所有学生信息 │ │ 0 退出系统 │ │ ========================================== │ │ 说明:通过数字选择菜单 │ ╚———————————————————————╝ ''') def insert(): continue_ = 'y' while continue_ == 'y': ID = input("请输入 ID(如 1001):") name = input("请输入名字:") chinese = input("请输入语文成绩:") while not is_float(chinese): chinese = input("输入无效!请重新输入语文成绩:") chinese = float(chinese) math = input("请输入数学成绩:") while not is_float(math): math = input("输入无效!请重新输入数学成绩:") math = float(math) english = input("请输入英语成绩:") while not is_float(english): english = input("输入无效!请重新输入英语成绩:") english = float(english) score = [{'id': ID, 'name': name, 'chinese': chinese, 'math': math, 'english': english}] if not isfile("students.pkl"): file = open("students.pkl", "ab") pickle.dump(score, file) file.close() else: file = open("students.pkl", "rb+") score_list = pickle.load(file) score_list.extend(score) file.close() file = open("students.pkl", "wb") pickle.dump(score_list, file) file.close() continue_ = input("是否继续添加?(y/n)") while continue_ not in ['y', 'n']: continue_ = input("输入无效!是否继续添加?(y/n)") else: print("学生信息录入完毕!") def search(): continue_ = 'y' while continue_ == 'y': if not isfile("students.pkl"): print("没有任何数据!") return way = input("按ID查输入1;按姓名查输入2:") while way not in ['1', '2']: way = input("输入无效!按ID查输入1;按姓名查输入2:") if way == '1': students = pickle.load(open("students.pkl", "rb")) ID = input("请输入学生 ID:") result = [] for each in students: if each['id'] == ID: result.append(each) else: students = pickle.load(open("students.pkl", "rb")) name = input("请输入学生姓名:") result = [] for each in students: if each['name'] == name: result.append(each) result_str = '' result_str += 'ID'.center(6) result_str += '姓名'.center(12) result_str += '语文'.center(10) result_str += '数学'.center(10) result_str += '英语'.center(10) result_str += '总成绩'.center(11) for i in result: result_str += "n" result_str += i['id'].center(6) result_str += i['name'].center(12) result_str += str(i['chinese']).center(12) result_str += str(i['math']).center(12) result_str += str(i['english']).center(12) result_str += str(i['chinese'] + i['math'] + i['english']).center(13) print(result_str) continue_ = input("是否继续查询 (y/n)?") while continue_ not in ['y', 'n']: continue_ = input("输入无效!是否继续查询 (y/n)?") def delete(): if not isfile("students.pkl"): print("没有学生信息!@_@") return continue_ = 'y' while continue_ == 'y': show() ID = input("请输入要删除的学生ID:") students = pickle.load(open("students.pkl", "rb")) result = [] for each in students: if each['id'] == ID: result.append(each) if not result: print(f"没有找到ID为 {ID} 的学生信息...") show() else: for i in result: students.remove(i) if not students: remove("students.pkl") print(f"ID 为 {ID} 的学生已成功删除!不可继续删除!") return else: pickle.dump(students, open("students.pkl", "wb")) print(f"ID 为 {ID} 的学生已成功删除!") continue_ = input("是否继续删除 (y/n)?") while continue_ not in ['y', 'n']: continue_ = input("输入无效!是否继续删除 (y/n)?") def modify(): if not isfile("students.pkl"): print("没有学生信息!") return show() continue_ = 'y' while continue_ == 'y': ID = input("请输入要修改的学生ID:") students = pickle.load(open("students.pkl", "rb")) result = [] for each in students: if each['id'] == ID: result.append(each) if not result: print(f"没有找到ID为 {ID} 的学生信息...") show() elif len(result) != 1: print("ID 有相同的情况!") else: students.remove(result[0]) name = input("请输入名字:") chinese = input("请输入语文成绩:") while not is_float(chinese): chinese = input("输入无效!请重新输入语文成绩:") chinese = float(chinese) math = input("请输入数学成绩:") while not is_float(math): math = input("输入无效!请重新输入数学成绩:") math = float(math) english = input("请输入英语成绩:") while not is_float(english): english = input("输入无效!请重新输入英语成绩:") english = float(english) score = {'id': ID, 'name': name, 'chinese': chinese, 'math': math, 'english': english} students.append(score) pickle.dump(students, open("students.pkl", "wb")) continue_ = input("是否继续修改 (y/n)?") while continue_ not in ['y', 'n']: continue_ = input("输入无效!是否继续修改 (y/n)?") def sort(): if not isfile("students.pkl"): print("没有学生信息!") return show() reverse = input("请选择(0升序;1降序):") while reverse not in ['0', '1']: reverse = input("输入无效!请选择(0升序;1降序):") reverse = bool(int(reverse)) students = pickle.load(open("students.pkl", "rb")) way = input("请选择排序方式" "(1按语文成绩排序;" "2按数学成绩排序;" "3按英语成绩排序;" "0按总成绩排序):") while way not in ['0', '1', '2', '3']: way = input("输入无效!请选择排序方式" "(1按语文成绩排序;" "2按数学成绩排序;" "3按英语成绩排序;" "0按总成绩排序):") if way == '0': def condition(x): return x['chinese'] + x['math'] + x['english'] elif way == '1': def condition(x): return x['chinese'] elif way == '2': def condition(x): return x['math'] else: def condition(x): return x['english'] result = sorted(students, key=condition, reverse=reverse) result_str = '' result_str += 'ID'.center(6) result_str += '姓名'.center(12) result_str += '语文'.center(10) result_str += '数学'.center(10) result_str += '英语'.center(10) result_str += '总成绩'.center(11) for i in result: result_str += "n" result_str += i['id'].center(6) result_str += i['name'].center(12) result_str += str(i['chinese']).center(12) result_str += str(i['math']).center(12) result_str += str(i['english']).center(12) result_str += str(i['chinese'] + i['math'] + i['english']).center(13) print(result_str) def total(): try: print("一共有", str(len(pickle.load(open("students.pkl", "rb")))), "名学生!") except FileNotFoundError: print("没有学生信息!") def show(): if not isfile("students.pkl"): print("没有学生信息!@_@") return result = pickle.load(open("students.pkl", "rb")) result_str = '' result_str += 'ID'.center(6) result_str += '姓名'.center(12) result_str += '语文'.center(10) result_str += '数学'.center(10) result_str += '英语'.center(10) result_str += '总成绩'.center(11) for i in result: result_str += "n" result_str += i['id'].center(6) result_str += i['name'].center(12) result_str += str(i['chinese']).center(12) result_str += str(i['math']).center(12) result_str += str(i['english']).center(12) result_str += str(i['chinese'] + i['math'] + i['english']).center(13) print(result_str) def is_float(number): try: float(number) except ValueError: return False else: return True if __name__ == '__main__': try: main() except baseException as e: print("啊哦,出错了 O_O") print(e) input("请按 |



