我在这项任务中遇到了一些麻烦,关于计算员工支付费用就像写一个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)
答案 0 :(得分: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
。
答案 1 :(得分:2)
print("you earn", (hours + max(hours - 40, 0) * 0.5) * rate)
或高尔夫版
print("you earn", (hours*3-min(40,hours))*rate/2)
答案 2 :(得分:1)
您可以使用:
pay = rate * min(hours, 40)
if hours > 40:
pay += rate * 1.5 * (hours - 40)
根据工作小时数调整工资计算。
您应该熟悉this resource。
答案 3 :(得分:0)
一些提示:
它是rate * 1.5
,而不是rate + 1.5
。该费率仅适用于过去 40小时,因此在前40个小时内,您应用常规费率:
if hours <= 40:
total = hours * rate
else:
total = 40 * rate + (hours - 40) * (1.5 * rate)