python if和else语句计算员工薪酬
我在这项任务中遇到了一些问题,它是关于计算员工工资的。它就像编写一个Python程序,提示用户输入小时工资和工作小时数,并计算工资金额。任何超过40小时的工作时间都按一次半(正常时薪的1.5倍)支付。使用if/else编写程序的一个版本到目前为止我的代码是这样的python if和else语句计算员工薪酬,python,Python,我在这项任务中遇到了一些问题,它是关于计算员工工资的。它就像编写一个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: pri
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)
hours=int(输入('你工作了多少小时?'))
比率=1.50
费率=(小时/2)+小时*(费率+1.5)
如果小时一些提示:
- 您还需要提示用户输入他们的小时费率
- 这是
rate*1.5
,而不是rate+1.5
。该费率仅适用于40小时后的小时数,因此在前40小时,您将应用常规费率:
if hours <= 40:
total = hours * rate
else:
total = 40 * rate + (hours - 40) * (1.5 * rate)
如果小时数您可以使用:
pay = rate * min(hours, 40)
if hours > 40:
pay += rate * 1.5 * (hours - 40)
根据工作小时数调整工资计算
您可能应该熟悉。如果您需要从用户处输入小时数和费率,您可以这样做:
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
你的总工资是定期+加班
print("you earn", (hours + max(hours - 40, 0) * 0.5) * rate)
或者是高尔夫版本
print("you earn", (hours*3-min(40,hours))*rate/2)
至少尝试一下,或者解释一下你的问题是什么。正如你的问题本质上是“为我做家庭作业”一样,这个问题似乎离题了,因为它是关于一个家庭作业问题,迄今为止OP只做了很少的尝试。我希望你将来给我写工资单!如果我工作1小时,你付给我40。哈哈,混淆min
和max
完全算作一个一次失误,对吧?!如果
,甚至不需要。。。。只需使用:hours*rate+max(hours-40,0)*(1.5*rate)
@JonClements。这不应该是(0.5*费率)
?@gnibbler只有在加班费减半的情况下才可以?@JonClements,您已经从小时*费率中获得1
。
@gnibbler errr。。。。咳嗽-我只是检查学生是否正确测试了他们的代码!?:)看起来很像我的一个稍有错误的评论;)可能应使用浮动时间和费率