-7

我在这项任务中遇到了一点麻烦,它关于计算员工的工资就像编写一个 Python 程序,提示用户输入每小时费率和工作小时数并计算工资金额。任何超过 40 小时的工作时间按时支付一半(正常小时费率的 1.5 倍)。使用 if/else 编写程序的一个版本,到目前为止我的代码是这样的

hours = int(input('how many hours did you work? '))
rate = 1.50
rate = (hours/2)+hours*(rate+1.5)
if hours<40:
 print("you earn",rate)
4

4 回答 4

3

如果您需要同时输入用户的小时数费率,您可以这样做:

hours = int(input('how many hours did you work? '))
rate = int(input('what is your hourly rate? '))

然后,一旦有了这些变量,就可以从计算加班开始。

if hours > 40:
  # anything over 40 hours earns the overtime rate
  overtimeRate = 1.5 * rate
  overtime = (hours-40) * overtimeRate
  # the remaining 40 hours will earn the regular rate
  hours = 40
else:
  # if you didn't work over 40 hours, there is no overtime
  overtime = 0

然后计算正常时间:

regular = hours * rate

你的总工资是regular + overtime

于 2013-07-30T23:42:49.980 回答
2
print("you earn", (hours + max(hours - 40, 0) * 0.5) * rate)

或打高尔夫球的版本

print("you earn", (hours*3-min(40,hours))*rate/2)
于 2013-07-31T00:21:22.343 回答
1

你可以使用:

pay = rate * min(hours, 40)
if hours > 40:
    pay += rate * 1.5 * (hours - 40)

根据工作小时数调整薪酬计算。

您可能应该熟悉此资源

于 2013-07-30T23:41:02.597 回答
0

一些提示:

  • 您还需要提示用户他们的小时费率。
  • rate * 1.5,不是rate + 1.5。该费率仅适用于40 小时后,因此在前40 小时内,您应用常规费率:

    if hours <= 40:
        total = hours * rate
    else:
        total = 40 * rate + (hours - 40) * (1.5 * rate)
    
于 2013-07-30T23:40:51.363 回答