-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython 第45题.py
More file actions
270 lines (244 loc) · 6.04 KB
/
Copy pathPython 第45题.py
File metadata and controls
270 lines (244 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import json
# ==========================================
# Python 综合项目(第45题)
# 员工绩效奖金管理系统 v1.0
# ==========================================
employees = [
{
"id": 1001,
"name": "张三",
"department": "财务部",
"salary": 8500,
"performance": 92
},
{
"id": 1002,
"name": "李四",
"department": "采购部",
"salary": 9200,
"performance": 81
},
{
"id": 1003,
"name": "王五",
"department": "财务部",
"salary": 7800,
"performance": 75
}
]
# ==========================================
# 任务要求
# ==========================================
#
# 显示系统标题
print("一、显示系统标题")
print("==========================================\n员工绩效奖金管理系统 v1.0\n==========================================\n")
# 二、
# 显示全部员工信息
#
# 输出内容至少包含:
# 工号
# 姓名
# 部门
# 工资
# 绩效
print("二、显示全部员工信息")
for emp in employees:
print(
f"工号:{emp['id']} \n"
f"姓名:{emp['name']} \n"
f"部门:{emp['department']}\n"
f"工资:{emp['salary']} \n"
f"绩效:{emp['performance']}\n"
)
# 三、
# 新增一名员工
#
# 所有数据由 input() 输入
#
# 工号必须转换为 int
# 工资必须转换为 int
# 绩效必须转换为 int
#
# 如果:
# 工资 < 0
# 或
# 绩效不在 0~100
#
# 必须主动抛出异常
#
# 程序不能崩溃
print("三、新增一名员工")
success=False
try:
new_id = int(input("工号:"))
new_name = input("姓名:")
new_department = input("部门:")
new_salary = int(input("工资:"))
if new_salary < 0:
raise ValueError("工资输入错误!")
new_performance = int(input("绩效:"))
if new_performance < 0 or new_performance > 100:
raise ValueError("绩效输入错误!")
except Exception as e:
print(e)
else:
print("员工信息录入成功!")
success=True
#
#四、
# 录入成功后
# 自动加入 employees
print("四、录入成功后:")
if success:
employees.append(
{"id": new_id,
"name": new_name,
"department": new_department,
"salary": new_salary,
"performance": new_performance}
)
# 五、
# 根据绩效计算奖金
#
# 奖金规则:
#
# >=90
# 奖金 = 工资 × 20%
#
# >=80
# 奖金 = 工资 × 10%
#
# >=70
# 奖金 = 工资 × 5%
#
# <70
# 无奖金
print("# 五、根据绩效计算奖金")
def count_bonus(performance,salary):
if performance>=90:
bonus=salary*0.2
elif performance>=80:
bonus=salary*0.1
elif performance >=70:
bonus=salary*0.05
else:
bonus=salary*0
return bonus
for emp in employees:
performance=emp["performance"]
salary=emp["salary"]
bonus=count_bonus(performance,salary)
print(f"奖金:{bonus}")
emp["bonus"] = bonus
# 六、
# 为每位员工新增字段:
#
# bonus
print("六、为每位员工新增字段:在第五题末尾")
# 七、
# 按奖金从高到低排序
print("七、按奖金从高到低排序")
sorted_employees=sorted(employees,key=lambda x:x["bonus"],reverse=True)
# 八、
# 输出奖金排行榜
print(" 八、输出奖金排行榜")
for index,emp in enumerate(sorted_employees,start=1):
print(
f"第{index}名",
f"{emp['name']}",
f"奖金{emp['bonus']}"
)
# 九、
# 统计:
#
# 公司平均工资
#
# 公司平均绩效
#
# 奖金总金额
#
# 财务部平均工资
#
# 财务部平均绩效
print("九、统计:")
company_salary=[emp['salary'] for emp in employees]
mean_company_salary=sum(company_salary)/len(company_salary)
company_performance=[emp['performance'] for emp in employees]
mean_company_performance=sum(company_performance)/len(company_performance)
company_bonus=[emp['bonus'] for emp in employees]
total_bonus=sum(company_bonus)
finance_salary=[emp['salary'] for emp in employees if emp['department']=='财务部']
mean_finance_salary=sum(finance_salary)/len(finance_salary)
finance_performance=[
emp['performance']
for emp in employees
if emp['department']=='财务部'
]
mean_finance_performance=sum(finance_performance)/len(finance_performance)
print(f"公司平均工资:{mean_company_salary}")
print(f"公司平均绩效:{mean_company_performance}")
print(f"奖金总金额:{total_bonus}")
print(f"财务部平均工资:{mean_finance_salary}")
print(f"财务部平均绩效:{mean_finance_performance}")
# 十、
# 保存到
#
# employee_bonus.json
#
# 必须使用 JSON 格式保存
#
# ensure_ascii=False
# indent=2
print("十、保存")
import json
with open("employee_bonus.json","w",encoding="utf-8")as f:
json.dump(employees,f,ensure_ascii=False,indent=2)
# 十一、
# 再读取 employee_bonus.json
#
# 验证保存成功
print("十一、再读取 employee_bonus.json")
with open("employee_bonus.json","r",encoding="utf-8")as f:
content=json.load(f)
print(content)
# 十二、
# 定义一个函数
#
# load_data(filename)
#
# 要求:
#
# 文件不存在
# 返回空列表
#
# JSON格式错误
# 返回空列表
#
# 正常读取
# 返回数据
print("十二、定义一个函数")
def load_data(filename):
try:
with open(filename,"r",encoding="utf-8")as f:
content= json.load(f)
return content
except FileNotFoundError:
print("文件不存在!")
return []
except json.JSONDecodeError:
print("JSON格式错误!")
return []
data=load_data("employee_bonus.json")
print(data)
# 十三、
# 思考题(不用写代码)
#
# 如果以后员工人数从3人增加到30000人,
#
# 你的程序哪些地方还能保持不变?
#
# 哪些地方需要优化?
#
# 简述原因。
print("十三、思考题:\n答:可以保持不变:数据结构(employees),奖金计算规则,排序逻辑,JSON保存逻辑,统计逻辑;需要优化:输入方式(不能再一个一个input),数据来源(数据库),查询效率,函数拆分,类设计(后续面向对象),Pandas统计")