0

当我计算加班时间时,我接受了所有工作,但正常工资是错误的。我的错误在哪里我一直试图弄清楚如何让它工作几个小时。如果我投入 45 小时,我的正常工资是 X 45 而不是 40。

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 ()
4

1 回答 1

0

这是因为hours从未被重新定义。

main()您获得hoursfrom的值getinput()- 这是标准加班的总小时数。

hours被传递给calculate_hours(),但如果它超过 40,则在 main 的范围内永远不会改变。所以当你调用calculate_payregular()的原始值hours(可能超过 40)被传入。

您可以修复此问题,因为calculate_hours()在返回的变量中返回正常速率时间strtime,因此如果您更改此:

regular, totalpay  = calculate_payregular (hours, rate, overtime)

对此:

regular, totalpay  = calculate_payregular (strtime, rate, overtime)

它应该正确计算它。

于 2013-11-04T03:16:36.090 回答