1. 程式人生 > >簡單的工資管理系統小程序,只是練手

簡單的工資管理系統小程序,只是練手

工資 swa 修改 lar 存在 inf 查詢 strip() 練手

需求如下:

工資管理系統
aaa 100000
bbb 80000
ccc 50000
ddd 30000
-----以上是info.txt文件-----
實現效果:
從info.txt文件中讀取員工及其工資信息,最後將修改或增加的員工工資信息
也寫入原info.txt文件。
效果演示:
1. 查詢員工工資
2. 修改員工工資
3. 增加新員工記錄
4. 退出
  1 #!/usr/bin/env python
  2 #-*- coding:utf-8 -*-
  3 # @Time    : 2017/9/21 15:19
  4 # @Author  : lichuan
  5 # @File    : salary.py
  6 
  7 import  os
8 25 26 def select(read,write): 27 i=input("please input the employee name:").strip() 28 for line in read: 29 if i == line.split()[0]: 30 print("%s的工資是:%s" % (line.split()[0],line.split()[1])) 31 return 32 print("輸入錯誤,請重試!") 33 read.seek(0) 34
select(read, write) 35 36 def modify(read,write): 37 # import os 38 FLAG_TAG=True 39 m=input("請輸入要修改的員工姓名和工資,以空格分隔").strip().split() 40 if len(m) !=2: 41 print("輸入錯誤,請重試,以空格分隔!") 42 modify(read,write) 43 return 44 for i in read: 45 if
i.split()[0] == m[0] and m[1].isdigit(): 46 FLAG_TAG=False 47 i = "%s %s\n" % (m[0], m[1]) 48 write.write(i) 49 if FLAG_TAG: 50 print("輸入錯誤,請重試!") 51 read.seek(0) 52 write.seek(0) 53 modify(read,write) 54 else: 55 read.close() 56 write.close() 57 os.remove("info.txt") 58 os.rename(".swap", "info.txt") 59 60 61 62 def add(read,write): 63 # import os 64 a=input("請輸入員工姓名和工資,以空格分隔").strip().split() 65 if len(a) !=2: 66 print("輸入錯誤,請重試,以空格分隔!") 67 add(read,write) 68 return 69 70 for i in read: 71 if i.split()[0] == a[0]: 72 print("用戶已存在,請重新添加新用戶!") 73 read.seek(0) 74 write.seek(0) 75 add(read,write) 76 return 77 write.write(i) 78 write.write("\n%s %s" %(a[0],a[1])) 79 read.close() 80 write.close() 81 os.remove("info.txt") 82 os.rename(".swap","info.txt") 83 84 85 86 def out(read,write): 87 88 print("再見!") 89 # FLAG_TAG=False 90 91 item={ 92 1:"查詢員工工資", 93 2:"修改員工工資", 94 3:"增加新員工記錄", 95 4:"退出" 96 } 97 98 fun={ 99 1:select, 100 2:modify, 101 3:add, 102 4:out 103 } 104 105 FLAG_TAG=True 106 107 while FLAG_TAG: 108 for i in range(1,5): 109 print("%d, %s" %(i,item[i])) 110 choice=input("請選擇:").strip() 111 with open(info.txt,r,encoding="utf-8") as r_read,open(.swap,w,encoding="utf-8") as r_write: 112 if choice.isdigit() and int(choice) >=1 and int(choice)<4: 113 choice=int(choice) 114 fun[choice](r_read,r_write) 115 continue 116 elif choice.isdigit() and int(choice) == 4: 117 FLAG_TAG=False 118 fun[int(choice)](r_read,r_write) 119 break 120 else: 121 print("wrong input,try again!") 122 continue 123 FLAG_TAG=False

簡單的工資管理系統小程序,只是練手