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


    作业内容:

    实现效果:

    从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('
    ').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 + '
    ')
            f.close()
        elif index_user_choice == '4':
            sys.exit("再见")
        else:
            print('输入操作无效!')
    View Code

     info.txt文件内容:

    Alex 100000
    
    Rain 80000
    
    Egon 50000
    
    Yuan 30000

     

  • 相关阅读:
    Common Lisp 参数传递的几种形式
    EF架构~基于EF数据层的实现
    标准架构~业务层到底是否应该关注数据持久化的方式
    Redis学习笔记~Redis主从服务器,读写分离
    Redis学习笔记~是时候为Redis实现一个仓储了,RedisRepository来了
    MVVM架构~knockoutjs系列之数组的$index和$data
    【Oracle】-【sqlplus / as sysdba登录报错问题】-新用户使用sqlplus / as sysdba登录报错
    Android---App Widget(一)
    Mit 分布式系统导论,Distributed Systems ,lab1 -lab6 总结,实验一到实验六总结
    GDAL切割重采样遥感图像
  • 原文地址:https://www.cnblogs.com/sean-yao/p/7707498.html
Copyright © 2020-2023  润新知