student_info = [] # 定义一个加载已存在的学员信息的方法 def load_student_info(): try: with open("student_info.txt", "r") as file: for line in file: name, sex, phone = line.strip().split(",") student_info.append({"姓名": name, "性别": sex,"手机号": phone}) print("学员信息已加载!") except FileNotFoundError: print("未找到之前保存的学员信息文件,将创建新的文件。") # 定义一个保存学员信息到文本文件的方法 def save_student_info(): with open("student_info.txt", "w") as file: for student in student_info: file.write(f"{student['姓名']},{student['性别']},{student['手机号']}\n") #写入信息操作 print("学员信息已保存到文件!") while True: load_student_info() # 显示主界面 print("主界面:") print("1:添加学员") print("2:删除学员") print("3:修改学员信息") print("4:查询学员信息") print("5:显示所有学员信息") print("6:保存学员信息") print("7:退出系统") # 用户输入功能序号 choice = input("请输入您需要的功能序号:") # 1.添加学员 if choice == "1": name = input("请输入学员姓名:") sex = input("请输入学员性别:") phone = input("请输入学员手机号:") student_info.append({"姓名": name, "性别": sex, "手机号": phone}) print("学员添加成功!") # 2.删除学员 elif choice == "2": name = input("请输入要删除的学员姓名:") for student in student_info: if student["姓名"] == name: student_info.remove(student) print("学员删除成功!") break else: print("未找到该学员!") # 3.修改学员信息 elif choice == "3": name = input("请输入要修改的学员姓名:") for student in student_info: if student["姓名"] == name: new_name = input("请输入新的姓名(留空表示不修改):") new_sex = input("请输入新的年龄(留空表示不修改):") new_phone = input("请输入新的年龄(留空表示不修改):") if new_name: student["姓名"] = new_name if new_sex: student["年龄"] = new_sex if new_phone: student["年龄"] = new_phone print(f"学员信息修改成功!姓名: {new_name}, 性别: {new_sex}, 手机号: {new_phone}") break else: print("未找到该学员!") # 4.查询学员信息 elif choice == "4": name = input("请输入要查询的学员姓名:") for student in student_info: if student["姓名"] == name: print("学员信息:", student) break else: print("未找到该学员!") # 5.显示所有学员信息 elif choice == "5": print("所有学员信息:", student_info) # 6.保存学员信息 elif choice == "6": save_student_info() print("学员信息已保存!") # 7.退出系统 elif choice == "7": print("已退出学生信息管理系统!") break # 处理无效输入 else: print("无效的功能序号,请重新输入!")