直接上源代码吧。
# 利用文件设计一个学生管理系统 class Student(object): def __init__(self): self.db_name = 'student_db.txt' self.students = { '7120214132341': '吕不韦', '8120214134348': '李明', '9120214131249': '张岩', '3220214121340': '祁门', '2120214431341': '吴峰' } try: with open(self.db_name, 'rt', encoding='UTF-8') as file: self.students.clear() db_data = file.readlines() for line in db_data: line = [item.strip() for item in line.split(':') if line] self.students[line[0]] = line[1] except FileNotFoundError as e: print('未发现数据库,系统将创建新的数据文件。') self.update_db() def update_db(self): self.students_list = list(self.students.keys()) # 一个不好的文件更新实现 with open(self.db_name, 'wt', encoding='UTF-8') as file: for key, value in self.students.items(): file.write(f'{key}: {value}\n') def menu(self) -> None: """管理系统的菜单""" option = [ '欢迎进入学生管理系统', '1. 查询全部学生', '2. 查询特定学生', '3. 更新学生信息', '4. 插入新的学生', '5. 删除已有学生', '6. 清除全部学生', '7. 退出管理系统' ] for item in option: print('|', item.center(30, '-'), '|') while True: choice = input('请根据菜单输入操作序号:') if choice.isdigit(): if choice == '1': self.query_all_student() elif choice == '2': self.query_one_student() elif choice == '3': self.update_student() elif choice == '4': self.insert_new_student() elif choice == '5': self.remove_one_student() elif choice == '6': self.remove_all_student() elif choice == '7': break else: print('请输入正确的序号。') else: print('请输入数字。') def query_all_student(self) -> bool: """查询全部学生""" if not self.students: print('学生信息库是空的') return False for num in range(len(self.students_list)): student_sno = self.students_list[num] student_name = self.students.get(self.students_list[num]) print(f'{num+1} 学号: {student_sno}, 学名: {student_name}') return True def query_one_student(self, query_name='') -> bool: """查询特定学生""" if not query_name: query_name = input('请输入要查询的学生姓名: ') if not self.is_name_valid(query_name): return False for sno, name in self.students.items(): if query_name == name: print(f'{sno}: {name}') return True print('查无此人。') return False def update_student(self) -> bool: """更新一个学生信息""" sno = input('请输入要修改的学号: ') name = input('请输入学生姓名: ') if self.is_sno_valid(sno) and self.is_name_valid(name): if self.students.get(sno): self.students[sno] = name return True else: print('查无此人。') return False def insert_new_student(self) -> bool: """插入新的学生""" name = input('请输入学生的姓名: ') sno = input('请输入学生的学号: ') if self.is_sno_valid(sno) and self.is_name_valid(name): if self.students.get(sno): print('该学号已经存在。') return False self.students[sno] = name self.update_db() return True def remove_one_student(self) -> bool: """删除一个学生""" remove_name = input('请输入要删除的学生姓名: ') remove_sno = '' for sno, name in self.students.items(): if remove_name == name: remove_sno = sno break if remove_sno: self.students.pop(remove_sno) self.update_db() return True else: print('查无此人。') return False def remove_all_student(self) -> bool: """删除所有学生""" self.students.clear() self.update_db() if self.students: print('清除失败,请排查故障') return False print('清除成功') return True def is_name_valid(self, name: str) -> bool: """检查输入的姓名是否合法""" if not name: print('姓名不可为空') return False if len(name) > 100: print('输入的姓名太长') return False return True def is_sno_valid(self, sno) -> bool: if not sno.isdigit() or len(sno) < 5 or len(sno) > 20: print('学号只能是5-20位的数字') return False return True if __name__ == '__main__': Student().menu()