zoukankan      html  css  js  c++  java
  • 4_python之路之模拟工资管理系统

    python之路之模拟工资管理系统

     

    1.程序说明:Readme.txt

    1.程序文件:salary_management.py info.txt
    
    2.程序文件说明:salary_management.py-主程序    info.txt-存放数据
    
    3.python版本:python-3.5.3
    
    4.程序使用:将salary_management.py和info.txt放到同一目录下, python salary_management.py
    
    5.程序解析:
    
        (1)满足 1. 查询员工工资 2. 修改员工工资 3. 增加新员工记录 4. 退出 功能需求
    
    6.实现知识点:
        (1)对文件的增删改查操作
        (2)对列表字典的读写操作
        (3)对函数的使用操作
        (4)对全局变量和局部变量的使用操作
    
    7.程序执行结果:请亲自动手执行或者查看我的博客
    
    8.程序博客地址:

    2.程序代码:salary_management.py

    #!/usr/bin/env python
    # _*_ coding: utf-8 _*_
    # author:chenjianwen
    # email:1071179133@qq.com
    import sys,os
    
    ##定义操作列表
    ops = ['查询员工工资','修改员工工资','增加新员工记录','退出']
    
    info = {}
    def get_info():
        ##读取文件得到员工信息
        global info
        with open('info.txt',mode='r',encoding='utf-8') as f_r:
            for line in f_r:
                line = line.rstrip('
    ')
                if line:
                    name = line.split(' ')[0]
                    slary = line.split(' ')[1]
                    info[name] ={
                                'name':'%s'%name,
                                'slary':'%s'%slary,
                                'other':''
                            }
    
    ##定义写操作函数
    def write(name,slary):
        with open('info.txt', mode='a+', encoding='utf-8') as f_w:
            f_w.write('
    %s %s'%(name,slary))
    
    ##定义修改员工信息函数
    def replace(old_name,new_name,old_slary,new_slary):
        f_r = open('info.txt',mode='r',encoding='utf-8')
        f_w = open('info1.txt', mode='w+', encoding='utf-8')
        for line in f_r:
            line = line.strip()
            if old_name in line:
                line = line.replace(old_name,new_name)
                line = line.replace(old_slary,new_slary)
            f_w.write(line + '
    ')
        f_r.close()
        f_w.close()
    
    while True:
        get_info()
        ##打印操作菜单
        for key,ops_lists in enumerate(ops):
            print(key,ops_lists,)
        select = input("请选择操作序号:")
        if select.isdigit():
            pass
        else:
            print("请输入数字")
            continue
    
        if select.startswith('0'):
            input1 = input("请输入员工名字:")
            print("%s的工资是:%s" %(input1,info[input1]['slary']))
            continue
    
        elif select.startswith('1'):
            input1 = input("请输入修改员工的名字:")
            print("%s现在的信息是:名字:%s,工资:%s"%(input1,info[input1]['name'],info[input1]['slary']))
            input2 = input("名字修改为:")
            input3 = input("工资修改为:")
            replace(input1, input2,info[input1]['slary'], input3)
            os.remove('info.txt')
            os.rename('info1.txt','info.txt')
            print("员工信息修改成功")
            get_info()
            continue
    
        elif select.startswith('2'):
            input1 = input("请输入增加员工的名字:")
            input2 = input("请输入增加员工的工资:")
            write(input1,input2)
            print("新增员工信息成功")
            get_info()
            continue
    
        elif select.startswith('3'):
            print("退出成功")
            sys.exit(1)

    3.程序保存数据文件:info.txt

    chenjianwen04 88888
    chenjianwen05 888888
    chenjianwen22 222222222
    
    chenjianwen002 444444444444444
    chenjianwen06 6666666
    chenjianwen08 88888888888888888
    
    chenjianwen12 8888
    
    chenjianwen33 33333333

    4.程序执行效果:

    5.The end

    6.改用横向输出效果:

      Python 2 :

        print打印的时候,如果结尾有逗号,打出来时候不会换行。但是在python3里面就不行

      Python3:

        print最后加个参数end=""

  • 相关阅读:
    Linux 下的类似Windows下Everything的搜索工具
    windows和linux环境下制作U盘启动盘
    程序调试手段之gdb, vxworks shell
    LeetCode 1021. Remove Outermost Parentheses (删除最外层的括号)
    LeetCode 1047. Remove All Adjacent Duplicates In String (删除字符串中的所有相邻重复项)
    LeetCode 844. Backspace String Compare (比较含退格的字符串)
    LeetCode 860. Lemonade Change (柠檬水找零)
    LeetCode 1221. Split a String in Balanced Strings (分割平衡字符串)
    LeetCode 1046. Last Stone Weight (最后一块石头的重量 )
    LeetCode 746. Min Cost Climbing Stairs (使用最小花费爬楼梯)
  • 原文地址:https://www.cnblogs.com/chenjw-note/p/7772406.html
Copyright © 2011-2022 走看看