Python工资核算计算问题

时间:2013-11-04 03:09:25

标签: python

当我计算加班费时,我已经接受了所有工作。 我的错误在哪里我一直试图弄清楚如何让它工作几个小时。如果我在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 ()

1 个答案:

答案 0 :(得分:0)

因为hours永远不会被重新定义。

main()中,您从hours获得getinput()的值 - 这是标准超时的总小时数。

hours已传递给calculate_hours(),但如果超过40,则永远不会在主范围内更改。因此,当您致电calculate_payregular() hours的原始值时(可能超过40岁)被传入。

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

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

到此:

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

它应该正确计算。