Python工资计算问题
我有所有的工作,当我计算加班时,它做了正常的工资错误。 我的错误在哪里?我几个小时来一直在想如何使它工作。如果我投入45小时,我的固定工资是45倍,而不是40倍Python工资计算问题,python,Python,我有所有的工作,当我计算加班时,它做了正常的工资错误。 我的错误在哪里?我几个小时来一直在想如何使它工作。如果我投入45小时,我的固定工资是45倍,而不是40倍 def main(): hours, rate = getinput() strtime, overtimehr, overtime = calculate_hours (hours,rate) regular, totalpay = calculate_payregular (hours, rate,
def main():
hours, rate = getinput()
strtime, overtimehr, overtime = calculate_hours (hours,rate)
regular, totalpay = calculate_payregular (hours, rate, overtime)
calprint (rate, strtime, overtimehr, regular, overtime, totalpay)
def getinput():
print ()
print ('How many hours were worked?')
print ('Hours worked must be at least 8 and no more than 86.')
hours = float (input('Now enter the hours worked please:'))
while hours < 8 or hours > 86: #validate the hours
print ('Error--- The hours worked must be atleast 8 and no more than 86.')
hours = float (input('Please try again:'))
print ('What is the pay rate?')
print ('The pay rate must be at least $7.00 and not more than $50.00.')
rate = float (input('Now enter the pay rate:'))
while rate < 7 or rate > 50: #validate the payrate
print ('Error--- The pay rate must be at least $7.00 and not more than $50.00.')
rate = float (input('Please try again:'))
return hours, rate
def calculate_hours (hours,rate):
if hours < 40:
strtime = hours
overtimehr = 0
else:
strtime = 40
overtimehr = hours - 40
if hours > 40:
overtimerate = 1.5 * rate
overtime = (hours-40) * overtimerate
else:
overtime = 0
return strtime, overtimehr, overtime
def calculate_payregular (hours, rate, overtime):
regular = hours * rate
totalpay = overtime + regular
return regular, totalpay
def calprint (rate, strtime, overtimehr, regular, overtime, totalpay):
print (" Payroll Information")
print ()
print ("Pay rate $", format (rate, '9,.2f'))
print ("Regular Hours ", format (strtime, '9,.2f'))
print ("Overtime hours ", format (overtimehr, '9,.2f'))
print ("Regular pay $", format (regular, '9.2f'))
print ("Overtime pay $", format (overtime, '9.2f'))
print ("Total Pay $", format (totalpay, '9.2f'))
main ()
def main():
小时数,速率=getinput()
标准时间、超时小时数、加班时间=计算小时数(小时数、费率)
常规工资,总工资=计算工资常规工资(小时、费率、加班)
calprint(费率、标准时间、超期、定期、加班、总工资)
def getinput():
打印()
打印(“工作了多少小时?”)
打印('工作小时数必须至少为8小时且不超过86小时')
小时=浮动(输入('现在请输入工作小时:'))
当小时数<8或小时数>86时:#验证小时数
打印('错误---工作时间必须至少为8小时,且不超过86小时')
小时数=浮动(输入('请重试:'))
打印(‘工资率是多少?’)
打印('工资率必须至少为7.00美元,但不得超过50.00美元')
费率=浮动(输入('现在输入工资费率:'))
当费率<7或费率>50时:#验证付款费率
打印('错误-付款率必须至少为$7.00,但不得超过$50.00')
速率=浮动(输入('请重试:'))
返程时间、费率
def计算小时数(小时数、费率):
如果小时数小于40:
strtime=小时
超时小时=0
其他:
strtime=40
超时小时=小时-40
如果小时数>40:
超时率=1.5*率
加班=(小时-40)*超时
其他:
加班费=0
返回时间、超时、加班
def计算工资常规(小时、费率、加班):
常规=小时*费率
总工资=加班+定期工资
定期返回,全额支付
def calprint(费率、标准时间、超期、定期、加班、总工资):
打印(“工资单信息”)
打印()
打印(“支付费率$”,格式(费率,'9,.2f'))
打印(“固定时间”,格式(标准时间,'9,.2f'))
打印(“加班时间”,格式(overtimehr,'9,.2f'))
打印(“常规薪资$”,格式(常规,'9.2f'))
打印(“加班费$”,格式(加班费,'9.2f'))
打印(“Total Pay$”,格式(totalpay,'9.2f'))
主要()
这是因为小时数
从未被重新定义过
在main()
中,您可以从getinput()
获取hours
的值,这是标准和加班的总小时数
hours
被传递到calculate_hours()
,但如果超过40,则在main范围内不会更改。因此,当您调用calculate\u payregular()
时,hours
的原始值(可能超过40)被传入
您可以修复此问题,因为calculate_hours()
在返回的变量strtime
中返回正常速率时间,因此如果您更改此值:
regular, totalpay = calculate_payregular (hours, rate, overtime)
为此:
regular, totalpay = calculate_payregular (strtime, rate, overtime)
它应该能正确计算。在你的测试中,rate
的值是多少?好吧,现在我更困惑了。哦,我把它设置为无论什么时候都做小时而不是时间。我知道如果工作时间是40小时或40小时以下,工作就可以了,但如果工作时间超过40小时,工作时间就可以了。谢谢alot@Bigslimm21别担心。如果答案有帮助,您可以单击向上箭头指示,如果答案解决了您的问题,您可以单击绿色勾号指示。如果答案没有帮助,你也可以单击向下箭头,但我希望这次不是这样!它不会让我点击,但你帮了我很多