1

所以我想写关于如何计算员工工资的代码任何工作超过 40 岁的员工都会按次支付一半,所以听到我的代码

hours = int(input('how many hours did you work'))
hour = int(input('how many hours did you work overtime'))
rate = 1.50
pay = hours*rate

if hours>40:
    pay = 40*1.5+hour*(1.5*rate)
else:
    40<hours

print('you earned',pay)

我哥哥说它应该问你一次而不是两次所以也许可以帮忙

4

4 回答 4

1

只是为了澄清@jh314 的问题。

hours = float (input('how many hours did you work?'))  # < hours can be fractional
OT = 1.5        # OT calculation in US.  A constant for this purpose
WAGES = 16.00   #  hourly rate, also usually a float!
pay = min(hours, 40) * WAGES 
pay += max(hours - 40, 0) * WAGES * OT
print "You earned  $ %0.2f " % pay

对于一个真实的应用程序,您可能会使用员工的特定费率来计算它:

def calc_wages( hrs, rate):
    OT = 1.5
    pay = min(hours, 40) * rate
    pay += max(hours - 40, 0) * rate * OT
    return pay
于 2013-08-05T03:44:49.877 回答
1

如果您知道加班时间超过 40 小时,您可以将这个人的工作小时数与 40 小时数进行比较来计算加班时间,而不是单独询问用户加班时间。

于 2013-08-05T02:48:09.203 回答
1

您可以通过意识到它是hours - 40或零来计算加班时间,以较大者为准。此外,正常工作时间上限为 40 小时。所以在min这里max会有帮助:

hours = int(input('how many hours did you work'))
wageRate = 1.50
overtimeRate = wageRate * 1.5
pay = min(40, hours) * wageRate           # regular hours
pay += max(hours - 40, 0) * overtimeRate  # add overtime
print('you earned',pay)
于 2013-08-05T02:49:29.283 回答
1
hours = int(input('how many hours did you work? '))
rate = 1.5 # or whatever normal pay rate is
pay = rate * (hours + 0.5 * max(hours-40,0))
于 2013-08-05T02:51:35.853 回答