如何使用 for 循环结构来实现一个计算和统计员工薪酬的功能,分高中低三段统计相应人员数量和平均薪资
时间: 2024-10-22 16:05:55 浏览: 7
在Python中,你可以使用for循环结合条件判断来实现这个功能。假设你有一个员工列表,每个员工都有姓名和薪资。这里是一个简单的示例:
```python
# 假设我们有这样一个员工列表
employees = [
{"name": "员工A", "salary": 5000},
{"name": "员工B", "salary": 8000},
{"name": "员工C", "salary": 12000},
# 更多员工...
]
# 定义工资段的阈值
low_threshold = 4000
medium_threshold = 6000
# 初始化计数器和总薪资
low_count = 0
medium_count = 0
total_low_salary = 0
total_medium_salary = 0
# 使用for循环遍历员工
for employee in employees:
salary = employee["salary"]
# 判断并统计
if salary <= low_threshold:
low_count += 1
total_low_salary += salary
elif low_threshold < salary <= medium_threshold:
medium_count += 1
total_medium_salary += salary
# 计算平均薪资
average_low_salary = total_low_salary / low_count if low_count > 0 else 0
average_medium_salary = total_medium_salary / medium_count if medium_count > 0 else 0
# 输出结果
print(f"低薪员工数量:{low_count}, 平均薪资:{average_low_salary}")
print(f"中薪员工数量:{medium_count}, 平均薪资:{average_medium_salary}")
```
阅读全文