【发布时间】:2020-05-19 15:29:37
【问题描述】:
我被困在学校这个问题上的时间比我愿意承认的要长......我已经完成了大约 5 次不同的迭代来解决这个问题,我最近的一次低于这个问题。这是我正在上的一门课。
给定以下员工和薪水字典,创建个性化的薪水信息,让每位员工知道他们获得了 2% 的加薪和新的总薪水。
预期结果:
John, your current salary is 54000.00. You received a 2% raise. This makes your new salary 55080.0 Judy, your current salary is 71000.00. You received a 2% raise. This makes your new salary 72420.0 Albert, your current salary is 38000.00. You received a 2% raise. This makes your new salary 38760.0 Alfonzo, your current salary is 42000.00. You received a 2% raise. This makes your new salary 42840.0
employeeDatabase = {
'John': 54000.00,
'Judy': 71000.00,
'Albert': 38000.00,
'Alfonzo': 42000.00
}
我的许多尝试之一(我现在意识到我应该保存以前的尝试,我只是使用随机在线 IDE):
newdict = employeeDatabase.copy()
for x in newdict:
newsal = newdict[x]
newsal = newsal *.02 + newsal
for i in employeeDatabase:
print (i + ' your current salary is %s You received a 2%% raise. This makes your new salary %d' % (employeeDatabase[i], newsal))
【问题讨论】:
-
您似乎拥有我们所说的XY problem,因为您的问题陈述中没有任何内容表明您需要将新工资存储在任何地方(尽管也许您可能想要存储?)所以如果没有必要要存储新工资,无需创建
newdict。但是,如果您确实愿意,可以写employeeDatabase[x] = newsal来更新原始员工数据库。
标签: python dictionary