小项目: low版本的 员工信息程序:
### 附加两个文件1 user_info 和worker_info
flag = False 2 def logon(): #登录函数 3 global flag 4 usr = input('Username:').strip() 5 pwd = input('Password:').strip() 6 with open('user_info',encoding= 'utf-8') as f: 7 for line in f.readlines(): 8 if usr+'\t'+pwd == line.strip(): 9 print('登录成功') 10 flag = True 11 return 12 else: 13 print('输入错误') 14 15 16 def id_find(): # 支持输入ID 准确查找 17 find_id = input('请输入您要查找的员工信息的ID: ') 18 print('ID, Name, Age, Phone, Job:') 19 with open('worker_info','r+',encoding='utf-8') as f: 20 f.seek(0) 21 lines = f.readlines() 22 print(lines[int(find_id)-1]) 23 24 25 def age_find():# 支持输入两个age 来找范围里面的 信息 26 find_age_1 = input('请输入您要查找的大于某某年龄段的数字:') 27 find_age_2 = input('请输入您要查找的小于某某年龄段的数字:') 28 print('ID, Name, Age, Phone, Job:') 29 with open('worker_info','r+',encoding='utf-8') as f: 30 f.seek(0) 31 for line in f.readlines(): 32 lis = line.strip().split(',') 33 if int(lis[2]) > int(find_age_1) and int(lis[2]) < int(find_age_2): 34 print(line) 35 36 37 def kwargs_find():# 支持关键字模糊查找 38 find_kwargs = input('请输入您的关键字:') 39 print('ID, Name, Age, Phone, Job:') 40 with open('worker_info','r+',encoding='utf-8') as f: 41 for line in f.readlines(): 42 if find_kwargs in line: 43 print(line) 44 45 46 def warpper(fu):# 认证 47 def inner(): 48 if flag == False: 49 print('请先登录') 50 logon() 51 ret = fu() 52 return ret 53 return inner 54 55 56 @warpper 57 def find_worker(): # 查找函数 58 print('1, ID查找 \n2, 年龄段查找 \n3, 关键字模糊查找 \n4, 查看全部信息') 59 find_id = input('请输入您选择的查找方法:').strip() 60 if find_id == '1': 61 id_find() 62 elif find_id == '2': 63 age_find() 64 elif find_id == '3': 65 kwargs_find() 66 elif find_id == '4': 67 print('下面是所有员工信息:') 68 print('ID, Name, Age, Phone, Job:') 69 with open('worker_info','r+',encoding='utf-8') as f: 70 for i in f.readlines(): 71 print(i) 72 73 74 @warpper 75 def append_info():# 添加新员工信息 76 name = input('请输入新员工的名字:').strip() 77 age = input('请输入新员工的年龄:').strip() 78 phone = input('请输入新员工的电话:').strip() 79 job = input('请输入新员工的工作:').strip() 80 with open('worker_info', 'a+', encoding='utf-8') as f: 81 f.seek(0) 82 li_lines = f.readlines() 83 f.seek(0,2) 84 f.write(str(len(li_lines)+1)+',%s,%s,%s,%s\n' %(name,age,phone,job)) 85 86 87 @warpper 88 def pop_info():#删除指定ID的员工信息 89 pop_id = input('请输入您要删除的员工的ID: ') 90 print('删除成功') 91 with open('worker_info','r+',encoding='utf-8') as f: 92 f.seek(0) 93 lines = f.readlines() 94 lines[int(pop_id)-1] ='该员工已经被删除\n' 95 f.seek(0) 96 for i in lines: 97 f.write(i) 98 return 99 100 101 @warpper 102 def change_info(): 103 change_id = input('请输入您要修改的员工信息的ID: ') 104 name = input('请输入新员工的名字:') 105 age = input('请输入新员工的年龄:') 106 phone = input('请输入新员工的电话:') 107 job = input('请输入新员工的工作:') 108 print('修改成功') 109 with open('worker_info','r+',encoding='utf-8') as f: 110 f.seek(0) 111 lines = f.readlines() 112 lines[int(change_id)-1] = '%s,%s,%s,%s,%s\n' %(change_id, name, age, phone, job) 113 f.seek(0) 114 for i in lines: 115 f.write(i) 116 return 117 118 119 def choise(): 120 while 1: 121 print('员工信息操作:') 122 print('1, 登录\n2, 查找员工信息\n3, 添加新员工信息\n4, 删除员工信息\n5, 修改员工信息\n6, 退出程序') 123 choose = input('请输入您要操作的功能:').strip() 124 if choose == '1': 125 logon() 126 elif choose == '2': 127 find_worker() 128 elif choose == '3': 129 append_info() 130 elif choose == '4': 131 pop_info() 132 elif choose == '5': 133 change_info() 134 elif choose == '6': 135 return 136 choise()