我有一个简单的python程序,可根据小时数和工资率计算工资。我希望程序能够计算超过40小时的额外工作时间和一半。问题工作40小时。超过40小时不能正确计算:
hrs = raw_input("Enter hours: ")
rt = raw_input("Enter rate: ")
hours = float(hrs)
rate = float(rt)
if hours > 40:
extra_hours = hours - 40
extra_rate = rate * 1.5
pay = rate * hours + (extra_rate * extra_hours)
print "Pay:", pay
else:
pay = rate * hours
print "Pay:", pay
答案 0 :(得分:2)
您计算超过40小时的额外费率。您首先按正常费率计算所有小时,然后再增加1.5倍的额外费用。
使用:
extra_hours = hours - 40
extra_rate = rate * 1.5
pay = rate * 40 + (extra_rate * extra_hours)
或将额外费率限制在0.5:
extra_hours = hours - 40
extra_rate = rate * 0.5
pay = rate * hours + (extra_rate * extra_hours)
答案 1 :(得分:0)
pay = rate * 40 +(extra_rate * extra_hours)