码迷,mamicode.com
首页 > 其他好文 > 详细

工资系统--练习文件的操作(增改查)

时间:2017-10-09 22:26:05      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:inpu   内容   sdi   continue   odi   退出   文件   工资   exit   

‘‘‘
1、使用的是字典保存
2、没有进行异常捕获
3、必须创建info文件,文件内容格式为:
{‘name‘: ‘xiaoli‘, ‘salary‘: 9990}
{‘name‘: ‘xiaowang‘, ‘salary‘: 8000}
‘‘‘
def memu():
print("1. 查询员工工资")
print("2. 修改员工工资")
print("3. 增加新员工记录")
print("4. 退出")

def find_wages(_name):
with open("info","r",encoding="utf-8") as f:
flag = 0
for line in f:
if line.strip() == "":
continue
line = eval(line)
if line["name"] == _name:
print("\033[31;1m %s的工资是%d \033[0m"%(line["name"],line["salary"]) + ‘\n‘)
flag = 1
if flag == 0:
print("\033[36;1m 员工中没有%s \033[0m"%_name)

def modify_wages(_name):
list = []
with open("info","r",encoding="utf-8") as f:
for line in f:
flag = 0
if line.strip() == "":
continue
line = eval(line)
if line["name"] == _name:
salary = input("请输入修改后的工资 >>>")
if salary.isdigit():
salary = int(salary)
else:
print("\033[36;1m 输入错误 \033[0m")
line["salary"] = salary
print("\033[32;1m %s修改后的工资是%d \033[0m"%(line["name"],line["salary"]) + ‘\n‘)
else:
if flag == 0:
print("\033[36;1m 员工中没有%s \033[0m"%_name)
flag = 1
list.append(line)
with open("info","w",encoding="utf-8") as f1:
for temp in list:
f1.write(str(temp) + ‘\n‘)

def add_wages():
dict = {}
name = input("请输入你要新增的员工姓名 >>>")
salary = input("请输入新增员工的工资 >>>")
if salary.isdigit():
salary = int(salary)
else:
print("\033[36;1m 输入错误 \033[0m")
dict["name"] = name
dict["salary"] = salary
print("\033[35;1m 新增员工%s的工资是%d \033[0m" % (dict["name"], dict["salary"]) + ‘\n‘)
with open("info","a",encoding="utf-8") as f:
f.write(str(dict) + ‘\n‘)

while True:
memu()
choose = input("请输入你要进行的操作 >>>")
if choose.isdigit():
choose = int(choose)
else:
print("\033[36;1m 输入错误 \033[0m")
continue
if choose == 1:
name = input("请输入要查询的员工姓名 >>>")
find_wages(name)
elif choose == 2:
name = input("请输入要修改的员工姓名 >>>")
modify_wages(name)
elif choose == 3:
add_wages()
elif choose == 4:
exit()
else:
print("\033[36;1m 输入错误 \033[0m")

工资系统--练习文件的操作(增改查)

标签:inpu   内容   sdi   continue   odi   退出   文件   工资   exit   

原文地址:http://www.cnblogs.com/hqd2008/p/7643006.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!