0

我有这个程序正在尝试编写,但我无法让它停止一遍又一遍地询问输入。我的错误在哪里?任何帮助都可以;我是编程新手,所以这都是陌生的。

def main():
    hours,rate = getinput()
    strtime,overtimehr,overtime = calculate_hours(hours)
    regular,overtime,totalpay  = calculate_pay(regular,overtimehr,rate)


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

    getinput()

def calculate_hours(hours):
    if hours < 40:
        strtime = hours
        overtime = 0
    else:
        strtime = 40
    overtimehr = hours - 40

    if hours > 40:
        overtimerate = 1.5 * rate
        overtime = (hours-40) * overtimerate
        hours = 40
    else:
        overtime = 0

    return strtime, overtime, overtimehr

    calculate_hours(hours)

def calculate_payregular(regular,totalpay,rate):
    regular = hours * rate
    totalpay = overtime + regular

    return regular, totalpay,rate

    calculate_payregular(regular,totalpay,rate)


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'))

    calprint (rate, strtime, overtimehr, regular, overtime, totalpay)

main ()
4

1 回答 1

1

您的所有功能,包括getinput(),最后似乎都在调用自己,这可能是您的问题的原因。您的任何功能都不需要最后一行 - 我不确定您为什么认为自己需要。从所有这些中删除它。

我也希望return hours, rate应该是不缩进一级。

于 2013-11-03T22:01:57.637 回答