简体   繁体   English

如何选择一个数字范围来乘以每小时工资

[英]How to select a range of numbers to multiply an hourly wage by

Im reading in a file with employee information on it.我正在阅读一个包含员工信息的文件。 First name, last name, hours worked, hourly wage.名字、姓氏、工作时间、小时工资。 I need to calculate regular pay, so hourly wage * hours between and including 0 and 40. Also, time and a half which is between 41 and 45, inclusive.我需要计算固定工资,所以小时工资 * 小时数介于 0 和 40 之间(包括 0 和 40)。此外,时间半在 41 和 45 之间,包括 41 和 45。 My question is how do I only use the hours between 0 and 40 to get the regular pay?我的问题是我如何只使用 0 到 40 之间的时间来获得正常工资? This is my current code:这是我当前的代码:

def main():
print()
print("This program will compute payroll.")
print("Written by Josh Sollenberger.")
print()

filename = input("Please enter the emplyee file name: ")
infile = open(filename, 'r')
print()
for line in infile:
    first,last,hours,wage = line.split(',')
    while int(hours) >= 0 and int(hours) <= 40:
        regPay = float(wage)*hours
    print(first,last,hours,wage,regPay)

main()主要的()

This is telling me i'm referencing regPay before assignment.这告诉我我在分配之前引用了 regPay。

I've done this before, using min and max我以前做过这个,使用minmax

regPay = float(wage) * min(int(hours),40)
# min(hours,40) will return hours, unless hours > 40 then it will return 40
OTpay = float(wage) * OVERTIMERATE * max(int(hours)-40,0)
# max(hours-40,0) will return hours-40, unless hours <= 40 then it will return 0

The reason it's telling you that you're referencing regPay before assignment is because you're ONLY getting to that regPay = float(wage)*hours line (that assigns regPay ) when int(hours) is between 0 and 40. If it's >= 41, it never gets in there!它告诉您在分配之前引用regPay的原因是因为当int(hours)介于 0 和 40 之间时,您只会到达regPay = float(wage)*hours行(分配regPay )。如果是 > = 41,它永远不会进入!

The incredibly long don't ever do this version of this code would be:令人难以置信的长时间不要执行此代码的此版本将是:

OVERTIMERATE = 1.5 # maybe 2?
first,last,hours,wage = line.split(',')
hours, wage = int(hours), float(wage) # Decimal is better here, but...
OTpay, regPay = 0,0 # initialize
while hours > 40:
    hours -= 1
    OTpay += wage*OVERTIMERATE
while hours > 0:
    hours -= 1
    regPay += wage

EDIT PER COMMENTS按评论编辑

reg_hours = min(int(hours), 40)
timeandhalf_hours = min(max(int(hours)-reg_hours),0),5)
doubletime_hours = max(int(hours) - (reg_hours + timeandhalf_hours), 0)

regPay = float(wage) * reg_hours
timeandhalfPay = float(wage) * 1.5 * timeandhalf_hours
doubletime_hours = float(wage) * 2 * doubletime_hours

Again the long form:又是长格式:

total_pay = 0
hours = int(hours)
wage = float(wage)

for i in range(1,hours+1):
    if i <= 40:
        total_pay += wage
    elif i <= 45:
        total_pay += wage * 1.5
    else:
        total_pay += wage * 2

Here's a full version of how I'd approach the problem:这是我如何解决问题的完整版本:

for line in infile: 
    first, last, hours, wage = line.split(',')
    hours, wage = int(hours), float(wage)
    if hours > 0: 
        regHours = min(hours, 40) # limit to 40
        otHours = max(hours - 40, 0)
        otHours = min(otHours, 5) # limit to 5
        dblHours = max(hours - 45, 0) # no limit
        regPay = regHours * wage
        otPay = otHours * wage * 1.5
        dblPay = dblHours * wage * 2
    else:
        regPay = 0
        otPay = 0
        dblPay = 0
    totalPay = regPay + otPay + dblPay

    print(first, last, hours, wage, totalPay)

And some quick results:以及一些快速的结果:

('chris', 'a', 50, 10.0, 575.0)
('chris', 'b', 41, 10.0, 415.0)
('chris', 'c', 35, 10.0, 350.0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM