• 工资系统--练习文件的操作(增改查)


    '''
    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")
  • 相关阅读:
    PC端圣诞树下载
    win7开机一直在正在启动windows界面怎么办?
    EFI、UEFI、MBR、GPT的区别
    进入BIOS中,设置U盘启动
    CSS3摆动动画效果
    比特币钱包搭建与使用
    自动校时工具
    windows7蓝屏0x000000c4
    如何使用webpack打包你的项目
    开源货币/比特币Multiminer、bitrade、bitcoinjs-lib、python-bitcoinrpc介绍
  • 原文地址:https://www.cnblogs.com/hqd2008/p/7643006.html
Copyright © 2020-2023  润新知