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 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
|
import os
filename = "student.txt"
def main(): while True: menm() choice = int(input("请输入")) if choice in [0, 1, 2, 3, 4, 5, 6, 7]: if choice == 0: answer = input("您确定退出系统吗?y/n") if answer == "y" or answer == "Y": print("谢谢使用") break else: continue elif choice == 1: insert() elif choice == 2: search() elif choice == 3: delete() elif choice == 4: modify() elif choice == 5: sort() elif choice == 6: total() elif choice == 7: show()
def menm(): print("=======================学生信息管理系统=====================") print("=======================功能菜单===========================") print("\t\t\t\t\t1.录入学生信息") print("\t\t\t\t\t2.查找学生信息") print("\t\t\t\t\t3.删除学生信息") print("\t\t\t\t\t4.修改学生信息") print("\t\t\t\t\t5.排序") print("\t\t\t\t\t6.统计学生总人数") print("\t\t\t\t\t7.显示所有学生信息") print("\t\t\t\t\t0.退出")
def insert(): student_list = [] while True: id = input("请输入id:") if not id: break name = input("请输入姓名:") if not name: break
try: english = int(input("请输入英语成绩")) python = int(input("请输入python成绩:")) java = int(input("请输入java成绩:")) except: print("输入无效,不是整数类型,请重新输入:") continue student = {"id": id, "name": name, "english": english, "python": python, "java": java} student_list.append(student) answer = input("是否继续相加") if answer == "y": continue else: break save(student_list) print("学生信息录入完毕!!!!")
def save(lst): try: stu_txt = open(filename, "a", encoding="utf-8") except: stu_txt = open(filename, "w", encoding="utf-8") for item in lst: stu_txt.write(str(item) + "\n") stu_txt.close()
def search(): student_query = [] while True: id = "" name = "" if os.path.exists(filename): mode = input("按id查找请输入1,按姓名查找请输入2:") if mode == "1": id = input("请输入学生id:") elif mode == "2": name = input("请输入学生的姓名:") else: print("您的输入有误,请重新输入") search() with open(filename, "r", encoding="utf-8") as rfile: student = rfile.readlines() for item in student: d = dict(eval(item)) if id != "": if d["id"] == id: student_query.append(d) elif name != "": if d["name"] == name: student_query.append(d) show_student(student_query) student_query.clear() anser = input("是否要继续查询?y/n\n") if anser == "y": continue else: break else: print("暂未保存学员信息") return
def show_student(lst): if len(lst) == 0: print("没查到学生信息,无数据显示!!!") return format_title = "{:^6}\t{:^12}\t{:^8}\t{:^01}\t{:^10}\t{:^8}" print(format_title.format("ID", "姓名", "英语成绩","python成绩", "java成绩", "总成绩")) format_data = "{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}" for item in lst: print(format_data.format(item.get("id"), item.get("name"), item.get("english"), item.get("python"), item.get("java"), int(item.get("english")) + int(item.get("python")) + int(item.get("java")) ))
def delete(): while True: student_id = input("请输入要删除学生的id:") if student_id != "": if os.path.exists(filename): with open(filename, "r", encoding="utf-8")as file: student_old = file.readlines() else: student_old = [] flag = False if student_old: with open(filename, "w", encoding="utf-8") as wfile: d = {} for item in student_old: d = dict(eval(item)) if d["id"] != student_id: wfile.write(str(d) + "\n") else: flag = True if flag: print(f"id为{student_id}的学生信息已被删除") else: print(f"没有找到id为{student_id}的学生信息") else: print("无学生信息") break show() answer = input("是否继续删除?y/n") if answer == "y": continue else: break
def modify(): show() if os.path.exists(filename): with open(filename, "r", encoding="utf-8") as rfile: student_old = rfile.readlines() else: return student_id = input("请输入要修改学生的id:") with open(filename, "w", encoding="utf-8") as wfile: for item in student_old: d = dict(eval(item)) if d["id"] == student_id: print("找到学生信息了,可以修改相关信息了!") while True: try: d["name"] = input("请输入姓名:") d["english"] = input("请输入英语成绩:") d["python"] = input("请输入Python成绩:") d["java"] = input("请输入java成绩:") except: print("您输入有误,请重新输入!!!") else: break wfile.write(str(d) + "\n") print("修改成功!!!") else: wfile.write(str(d) + "\n") answer = input("是否要继续修改其他学生信息?y/n") if answer == "y": modify()
def sort(): show() if os.path.exists(filename): with open(filename,"r",encoding="utf-8") as rfile: student_list=rfile.readlines() student_new=[] for item in student_list: d=dict(eval(item)) student_new.append(d) else: return asc_or_desc=input("请选择(0.升序 1.降序):") if asc_or_desc=="0": asc_or_desc_bool=False elif asc_or_desc=="1": asc_or_desc_bool=True else: print("您的输入有误,请重新输入") sort() mode=input("请选择排序方式(1,按英语成绩排序 2,按python程序排序 3,按Java程序排序 0,按照总程序排序)") if mode=="1": student_new.sort(key=lambda x:int(x["english"]),reverse=asc_or_desc_bool) elif mode=="2": student_new.sort(key=lambda x:int(x["python"]),reverse=asc_or_desc_bool) elif mode=="3": student_new.sort(key=lambda x:int(x["java"]),reverse=asc_or_desc_bool) elif mode=="0": student_new.sort(key=lambda x: int(x["english"])+int(x["python"])+int(x["java"]),reverse=asc_or_desc_bool) else: print("您的输入有误,请从新输入:") sort() show_student(student_new)
def total(): if os.path.exists(filename): with open(filename,"r",encoding="utf-8") as rfile: student=rfile.readlines() if student: print(f"一共有{len(student)}名学生") else: print("还没有录入学生信息") else: print("暂未保存数据信息.....")
def show(): student_lst=[] if os.path.exists(filename): with open(filename,"r",encoding="utf-8") as rfile: student=rfile.readlines() for item in student: student_lst.append(eval(item)) if student_lst: show_student(student_lst) else: print("暂未保存过数据!!")
if __name__ == "__main__": main()
|