'''
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(" 33[31;1m %s的工资是%d 33[0m"%(line["name"],line["salary"]) + '
')
flag = 1
if flag == 0:
print(" 33[36;1m 员工中没有%s 33[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(" 33[36;1m 输入错误 33[0m")
line["salary"] = salary
print(" 33[32;1m %s修改后的工资是%d 33[0m"%(line["name"],line["salary"]) + '
')
else:
if flag == 0:
print(" 33[36;1m 员工中没有%s 33[0m"%_name)
flag = 1
list.append(line)
with open("info","w",encoding="utf-8") as f1:
for temp in list:
f1.write(str(temp) + '
')
def add_wages():
dict = {}
name = input("请输入你要新增的员工姓名 >>>")
salary = input("请输入新增员工的工资 >>>")
if salary.isdigit():
salary = int(salary)
else:
print(" 33[36;1m 输入错误 33[0m")
dict["name"] = name
dict["salary"] = salary
print(" 33[35;1m 新增员工%s的工资是%d 33[0m" % (dict["name"], dict["salary"]) + '
')
with open("info","a",encoding="utf-8") as f:
f.write(str(dict) + '
')
while True:
memu()
choose = input("请输入你要进行的操作 >>>")
if choose.isdigit():
choose = int(choose)
else:
print(" 33[36;1m 输入错误 33[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(" 33[36;1m 输入错误 33[0m")