标签:除了 return dea span int() write 函数 print sea
# 函数版学员管理系统 class System1(): info = [] def info_print(self): print(‘请选择功能:‘ + ‘-‘ * 30) print(‘ 1、添加学员\t‘, ‘2、删除学员\t‘, ‘3、修改学员\t‘, ‘4、查询学员\n‘, ‘5、显示学员\t‘, ‘6、保存信息\t‘, ‘7、退出系统\t‘, ‘8、读取文件\n‘, ‘-‘ * 40) def del_info(self): global info del_name = input(‘请输入要删除的学员的姓名:‘) for i in System1.info: # 2.3 存在则删除(列表里面的字典),break:这个系统不允许重名,删除了一个后面的不需要再遍历;思路2:如果所有值只要有一个在字典的值里,则删除。if dea_name=i.values:去掉break。 if del_name == i[‘name‘]: System1.info.remove(i) break else: print(‘该学员不存在‘) print(System1.info) def modify_info(self): modify_name = input(‘请输入要修改的学员的姓名:‘) global info for i in System1.info: if modify_name == i[‘name‘]: # 将tel这个key修改值,并终止此循环 i[‘tel‘] = input(‘请输入新的手机号:‘) break else: print(‘该学员不存在‘) print(System1.info) def search_info(self): search_name = input(‘请输入要查询的学员的姓名:‘) # 2. 存在打印这个学员的信息;不存在则提示 global info for i in System1.info: if search_name == i[‘name‘]: print(‘查询到的学员信息如下---------------‘) print(f"学员的学号是{i[‘id‘]}, 姓名是{i[‘name‘]}, 手机号是{i[‘tel‘]}") break else: print(‘查无此人...‘) def print_all(self): print(‘学号\t姓名\t手机号‘) for i in System1.info: print(f"{i[‘id‘]}\t{i[‘name‘]}\t{i[‘tel‘]}") # print(info) # 系统功能需要循环使用,直到用户输入6,才退出系统 def begin(self): while True: self.info_print() user_num = int(input(‘请输入功能序号:‘)) # 3. 按照用户输入的功能序号,执行不同的功能(函数) if user_num == 1: self.add_info() elif user_num == 2: self.del_info() elif user_num == 3: self.modify_info() elif user_num == 4: self.search_info() elif user_num == 5: self.print_all() elif user_num == 6: self.save() elif user_num == 8: self.read_file() elif user_num == 7: exit_flag = input(‘确定要退出吗?yes or no‘) if exit_flag == ‘yes‘: break else: print(‘输入的功能序号有误‘) def add_info(self): new_id, new_name, new_tel = input(‘请输入学号:‘), input(‘请输入姓名:‘), input(‘请输入手机号:‘) for i in System1.info: if new_name == i[‘name‘]: print(‘此用户已经存在‘) return # 2.2 如果输入的姓名不存在,添加数据 info_dict = {} info_dict[‘id‘], info_dict[‘name‘], info_dict[‘tel‘] = new_id, new_name, new_tel System1.info.append(info_dict) print(System1.info) def save(self): new_id, new_name, new_tel = input(‘请输入学号:‘), input(‘请输入姓名:‘), input(‘请输入手机号:‘) str1 = open(‘file1.txt‘, ‘w‘) str1.write(new_id + ‘\n‘ + new_name + ‘\n‘ + new_tel) str1.close() def read_file(self): str1 = open(‘file1.txt‘, ‘r‘) str2 = str1.read() print(‘读取内容为:\n‘ + str2) str1.close() if __name__ == ‘__main__‘: s = System1() s.begin()
标签:除了 return dea span int() write 函数 print sea
原文地址:https://www.cnblogs.com/teark/p/14247972.html