1 print("欢迎来到HR人力资源管理理系统") 2 menu = ("1:查看员工信息", "2:添加员工信息", "3:修改员工信息", "4:删除员工信息", "Q:退出") 3 def menu_op(): #定义菜单函数,选择操作 4 print(menu) 5 msg = input("请做出你的选择或退出:") 6 if msg=="1": 7 look() 8 elif msg=="2": 9 add() 10 elif msg=="3": 11 amend() 12 elif msg=="4": 13 re() 14 elif msg.upper()=="Q": 15 exit() 16 else: 17 print("请重新输入!!") 18 menu_op() 19 def look(): 20 with open("emp.db", "r+", encoding="utf-8") as f: 21 people = f.readlines() #每行以列表形式,读取文件所有内容 22 #print(people) 23 msg = input("请输入你要查看的员工ID(Q进入菜单):") 24 if msg.isdigit(): 25 for i in people: # 26 if i[0] == msg: #根据ID选择列表内员工 27 print(i) 28 look() 29 elif msg.upper()=="Q": 30 menu_op() 31 else: 32 print("请正确输入!!") 33 def add(): 34 while True: 35 with open("emp.db","r+",encoding="utf-8") as f: 36 people = f.readlines() 37 f.seek(0) 38 print(people) 39 msg = input("请输入您的ID,姓名,薪水(用逗号分隔):") 40 #print(msg.split(",")[0]) 41 if msg.upper()=="Q": 42 menu_op() 43 elif len(msg.split(",")) !=3: 44 print("你的输入有误,请重新输入!!") 45 else: 46 for i in people: 47 if i.strip()==msg: 48 print("你输入的员工已存在!!") 49 add() 50 if len(people) < 1 or i.strip()!=msg: 51 f.seek(0,2) 52 f.write(msg + " ") 53 f.flush() 54 f.close() 55 add() 56 def amend(): 57 with open("emp.db", "r+", encoding="utf-8") as f: 58 people = f.readlines() 59 print(people) 60 msg = input("请输入你要修改的员工ID(Q退出):") 61 if msg.isdigit(): 62 for i in people: 63 if i.split(",")[0] == msg: 64 print(i) 65 msg1 = input("请重新输入您的ID,姓名,薪水(用逗号分隔,Q退出):") 66 if msg1.upper() == "Q": 67 amend() 68 elif len(msg1.split(",")) != 3: 69 print("你的输入有误,请重新输入!!") 70 else: 71 for i in people: 72 if msg==i[0]: 73 people.remove(i) 74 people.append(msg1+" ") 75 f.seek(0) 76 f.write("".join(people)) 77 f.flush() 78 f.close() 79 amend() 80 elif msg.upper() == "Q": 81 menu_op() 82 def re(): 83 with open("emp.db", "r+", encoding="utf-8") as f: 84 people = f.readlines() 85 print(people) 86 msg = input("请输入你要删除的员工ID(Q退出):") 87 if msg.isdigit(): 88 for i in people: 89 if msg == i[0]: 90 people.remove(i) 91 #people[int(msg)-1] = msg1 92 f.seek(0) 93 f.truncate() 94 f.write("".join(people)) 95 f.flush() 96 f.close() 97 re() 98 elif msg.upper()=="Q": 99 menu_op() 100 else: 101 print("请重新输入!!") 102 menu_op()