函数基础2

原始数据

# 员工列表(员工编号 部门编号 姓名 工资)
list_employees = [
    {"eid": 1001, "did": 9002, "name": "师父", "money": 60000},
    {"eid": 1002, "did": 9001, "name": "孙悟空", "money": 50000},
    {"eid": 1003, "did": 9002, "name": "猪八戒", "money": 20000},
    {"eid": 1004, "did": 9001, "name": "沙僧", "money": 30000},
    {"eid": 1005, "did": 9001, "name": "小白龙", "money": 15000},
]

需求

需求1:按格式打印员工的信息

def print_employee_info(name,eid,did,money):
    print("%s的员工编号是%s,部门编号是%s,月薪%s元." % (name,eid,did,money))

需求2:定义函数,打印所有员工信息

def print_all_employee_info():
    for item in list_employees:
        print_employee_info(item["name"],item["eid"],item["did"],item["money"])

需求3:定义函数,打印所有月薪大于2w的员工信息

def print_salary_20000_info():
    for item in list_employees:
        if item["money"]>20000:
            print_employee_info(item["name"],item["eid"],item["did"],item["money"])

需求4:定义函数,获取薪资最低的员工

def get_lowest_salary_employee_info():
    lowest_employee = list_employees[0]
    for i in range(1,len(list_employees)):
        if lowest_employee["money"] > list_employees[i]["money"]:
            lowest_employee = list_employees[i]
    return lowest_employee

需求5:定义函数,根据薪资对员工列表升序排序

def aescending_sort_by_salary():
    for r in range(len(list_employees)-1):
        for c in range(r+1,len(list_employees)):
            if list_employees[r]["money"] > list_employees[c]["money"]:
                list_employees[r]["money"],list_employees[c]["money"] = list_employees[c]["money"],list_employees[r]["money"]

 调用函数输出结果

print_all_employee_info()
print_salary_20000_info()
lowest_employee = get_lowest_salary_employee_info()
print("最低的员工是"+str(lowest_employee))
aescending_sort_by_salary()
print(list_employees)

师父的员工编号是1001,部门编号是9002,月薪60000元.
孙悟空的员工编号是1002,部门编号是9001,月薪50000元.
猪八戒的员工编号是1003,部门编号是9002,月薪20000元.
沙僧的员工编号是1004,部门编号是9001,月薪30000元.
小白龙的员工编号是1005,部门编号是9001,月薪15000元.
师父的员工编号是1001,部门编号是9002,月薪60000元.
孙悟空的员工编号是1002,部门编号是9001,月薪50000元.
沙僧的员工编号是1004,部门编号是9001,月薪30000元.
最低的员工是{'eid': 1005, 'did': 9001, 'name': '小白龙', 'money': 15000}
[{'eid': 1001, 'did': 9002, 'name': '师父', 'money': 15000}, {'eid': 1002, 'did': 9001, 'name': '孙悟空', 'money': 20000}, {'eid': 1003, 'did': 9002, 'name': '猪八戒', 'money': 30000}, {'eid': 1004, 'did': 9001, 'name': '沙僧', 'money': 50000}, {'eid': 1005, 'did': 9001, 'name': '小白龙', 'money': 60000}]

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值