#!/usr/bin/env python#-*- coding:utf-8 -*-#Author: Sean_Yao
"""工资管理系统作业"""
importsys
with open("info.txt", 'r', encoding="utf-8") as f:
file=list(f)
msg= '''1. 查询员工工资
2. 修改员工工资
3. 增加新员工记录
4. 退出'''exit_flag=Falsewhile notexit_flag:print(msg)
index_user_choice= input('>>:')if index_user_choice == '1':
with open("info.txt", 'r', encoding="utf-8") as f:
user_salary=f.readlines()
username= input("请输入要查询的员工姓名(例如:Alex):")for user_line inuser_salary:
(user,salary)= user_line.strip('\n').split()if username ==user:print('%s 的工资是:%s' %(username,salary))pass
elif index_user_choice == '2':
old_user= input("输入员工姓名(例如:Alex):").strip()for i infile:
file=i.strip().split()if old_user infile:
old_salary= file[1]
new_user,new_salary= input("请输入更改后员工姓名和工资,用空格分隔(例如:Alex 10):").strip().split()
with open("info.txt", "r", encoding="utf-8") as f:
lines=f.readlines()
with open("info.txt", "w", encoding="utf-8") as f_a:for line inlines:if old_user inline:
line=line.replace(old_user,new_user)
f_a.write(line)
f_a.close()
with open("info.txt", "r", encoding="utf-8") as f:
lines=f.readlines()
with open("info.txt", "w", encoding="utf-8") as f_b:for line inlines:if new_user inline:
line=line.replace(old_salary,new_salary)
f_b.write(line)
f_b.close()print("修改成功")elif index_user_choice == '3':
f= open('info.txt', 'r+', encoding='utf-8')
user_salary=f.readlines()
new_user_new_salary= input("请输入要增加的员工姓名和工资,共空格分割(例如:Eric 100000):")
f.write(new_user_new_salary+ '\n')
f.close()elif index_user_choice == '4':
sys.exit("再见")else:print('输入操作无效!')