码迷,mamicode.com
首页 > 编程语言 > 详细

Python作业工资管理系统(第三周)

时间:2017-10-22 01:37:36      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:lin   code   input   for   usr   ace   工资   new   als   

作业内容:

实现效果:

从info.txt文件中读取员工及其工资信息,最后将修改或增加的员工工资信息也写入原info.txt文件。

效果演示:

1. 查询员工工资

2. 修改员工工资

3. 增加新员工记录

4. 退出

 

思路:

进行文件读写,将读或写的数据利用strip和split方法,对文件内容进行增改查

流程图:(待更新)

代码:

技术分享
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Sean_Yao
"""工资管理系统作业"""
import  sys
with open("info.txt", r, encoding="utf-8") as f:
    file = list(f)
msg = ‘‘‘
1. 查询员工工资
2. 修改员工工资
3. 增加新员工记录
4. 退出
‘‘‘
exit_flag = False
while not exit_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 in user_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 in file:
                file = i.strip().split()
                if old_user in file:
                    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 in lines:
                            if old_user in line:
                                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 in lines:
                            if new_user in line:
                                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(输入操作无效!)
View Code

 info.txt文件内容:

Alex 100000

Rain 80000

Egon 50000

Yuan 30000

 

 

Python作业工资管理系统(第三周)

标签:lin   code   input   for   usr   ace   工资   new   als   

原文地址:http://www.cnblogs.com/sean-yao/p/7707498.html

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