使用字典完成员工数据记录,包含姓名、部门、工资和级别,并通过for循环,对所有级别为1级的员工,级别上升1级,薪水增加1000元。
# 组织字典记录数据
info_dict = {
"刘备": {
"部门": "系统部",
"工资": 7000,
"级别": 3
},
"张飞": {
"部门": "中试部",
"工资": 6000,
"级别": 1
},
"关羽": {
"部门": "软件部",
"工资": 9000,
"级别": 1
},
"曹操": {
"部门": "人事部",
"工资": 2000,
"级别": 6
},
"周瑜": {
"部门": "财务部",
"工资": 7000,
"级别":1
}
}
print(f"对员工升职加薪前的结果:{info_dict}")
# for循环遍历字典
for name in info_dict:
# if条件判断符合条件员工
if info_dict[name]["级别"] == 1:
# 升职加薪操作
# 获取员工的信息字典
employee_info_dict = info_dict[name]
# 修改员工信息
employee_info_dict["级别"] = 2 # 级别+1
employee_info_dict["工资"] += 1000 # 工资+1000
# 将员工信息更新回info_dict
info_dict[name] = employee_info_dict
# 输出结果
print(f"对员工升职加薪后的结果:{info_dict}")
运行结果:
对员工升职加薪前的结果:{‘刘备’: {‘部门’: ‘系统部’, ‘工资’: 7000, ‘级别’: 3}, ‘张飞’: {‘部门’: ‘中试部’, ‘工资’: 6000, ‘级别’: 1}, ‘关羽’: {‘部门’: ‘软件部’, ‘工资’: 9000, ‘级别’: 1}, ‘曹操’: {‘部门’: ‘人事部’, ‘工资’: 2000, ‘级别’: 6}, ‘周瑜’: {‘部门’: ‘财务部’, ‘工资’: 7000, ‘级别’: 1}}
对员工升职加薪后的结果:{‘刘备’: {‘部门’: ‘系统部’, ‘工资’: 7000, ‘级别’: 3}, ‘张飞’: {‘部门’: ‘中试部’, ‘工资’: 7000, ‘级别’: 2}, ‘关羽’: {‘部门’: ‘软件部’, ‘工资’: 10000, ‘级别’: 2}, ‘曹操’: {‘部门’: ‘人事部’, ‘工资’: 2000, ‘级别’: 6}, ‘周瑜’: {‘部门’: ‘财务部’, ‘工资’: 8000, ‘级别’: 2}}