没有得到正确的输出python

时间:2017-03-20 01:36:01

标签: python

尝试编写一个程序,将其作为常规时间显示长达40小时,并将其作为超时时间显示。

目前获取的输出输入超过40并输出-1作为常规时间,1输出为加班。似乎无法弄清楚我哪里出错了。

def data():
    wage = float(input('What is your payrate? '))
    while wage < 7 or wage > 50:
        print('Invalid input. Wage must be between $7.00 and $51.00. Please try again.')
        rate = float(input('What is your payrate? '))
    hours = float(input('How many hours were worked? '))
    while hours < 8 or hours > 86:
        print('Invalid input. Hours worked must be between 8 and 86. Please try again.')
        hours = float(input('How many hours were worked? '))
    return hours, wage

def timeCalc(hours):
    if hours <= 40:
        return[hours, False]
    else:
        othour = hours - 40
        return[othour, True]

def payCalc(hours, wage):
    if hours[1]:
        othours = hours[1]
        otpay = othours * (wage * 1.5)
        regularpay = 40 * wage
        return otpay, regularpay, othours
    else:
        return None

def main():
    hours, wage = data()
    if payCalc(timeCalc(hours), wage) == None:
        print('      Payroll Information      ')
        print('Pay Rate: $%.2f' % wage)
        print('Regular Hours: %.0f' % hours)
        print('Overtime Hours: 0')
        print('Regular pay: $%.2f' % (hours * wage))
        print('Overtime pay: $0.00')
        print('')
        print('Total pay: $%.2f' % (hours * wage))
    else:
        otpay, regularpay, othours = payCalc(timeCalc(hours), wage,)
        print('      Payroll Information      ')
        print('Pay Rate: $%.2f' % wage)
        print('Regular Hours: %.0f' % (hours - othours))
        print('Overtime Hours: %.0f' % othours)
        print('Regular pay: $%.2f' % (hours * wage))
        print('Overtime pay: $%.2f' % otpay)
        print('Total pay: $%.2f' % (regularpay + otpay))

main()

输出看起来像这样(为了清晰起见,我添加了空格):

What is your payrate? 11.5
How many hours were worked? 48
  Payroll Information      
Pay Rate:        $11.50
Regular Hours:   47
Overtime Hours:  1
Regular pay:     $552.00
Overtime pay:    $17.25
Total pay:       $477.25

感谢任何帮助

2 个答案:

答案 0 :(得分:0)

在函数payCalc中,您需要编写othours = hours[0]而不是othours = hours[1]来获取小时数(因为现在它会为您提供数字中的True或False值)

def data():
    wage = float(input('What is your payrate? '))
    while wage < 7 or wage > 50:
        print('Invalid input. Wage must be between $7.00 and $51.00. Please try again.')
        rate = float(input('What is your payrate? '))
    hours = float(input('How many hours were worked? '))
    while hours < 8 or hours > 86:
        print('Invalid input. Hours worked must be between 8 and 86. Please try again.')
        hours = float(input('How many hours were worked? '))
    return hours, wage

def timeCalc(hours):
        if hours <= 40:
            return[hours, False]
        else:
            othour = hours - 40
            return[othour, True]

def payCalc(hours, wage):
    if hours[1]:
        othours = hours[0] # Change Code here
        otpay = othours * (wage * 1.5)
        regularpay = 40 * wage
        return otpay, regularpay, othours
    else:
        return None

def main():
    hours, wage = data()
    if payCalc(timeCalc(hours), wage) == None:
        print('      Payroll Information      ')
        print('Pay Rate: $%.2f' % wage)
        print('Regular Hours: %.0f' % hours)
        print('Overtime Hours: 0')
        print('Regular pay: $%.2f' % (hours * wage))
        print('Overtime pay: $0.00')
        print('')
        print('Total pay: $%.2f' % (hours * wage))
    else:
        otpay, regularpay, othours = payCalc(timeCalc(hours), wage,)
        print('      Payroll Information      ')
        print('Pay Rate: $%.2f' % wage)
        print('Regular Hours: %.0f' % (hours - othours))
        print('Overtime Hours: %.0f' % othours)
        print('Regular pay: $%.2f' % (hours * wage))
        print('Overtime pay: $%.2f' % otpay)
        print('Total pay: $%.2f' % (regularpay + otpay))

main()

这是输出

What is your payrate? 50

How many hours were worked? 50

Payroll Information      
Pay Rate: $50.00
Regular Hours: 40
Overtime Hours: 10
Regular pay: $2500.00
Overtime pay: $750.00
Total pay: $2750.00

希望这会有效!!

答案 1 :(得分:0)

payCalc函数,如果小时[1]改为小时[0]

,则更改
#!/usr/bin/env python
# coding:utf-8

'''黄哥Python'''


def data():
    wage = float(input('What is your payrate? '))
    while wage < 7 or wage > 50:
        print('Invalid input. Wage must be between $7.00 and $51.00. Please try again.')
        rate = float(input('What is your payrate? '))
    hours = float(input('How many hours were worked? '))
    while hours < 8 or hours > 86:
        print('Invalid input. Hours worked must be between 8 and 86. Please try again.')
        hours = float(input('How many hours were worked? '))
    return hours, wage


def timeCalc(hours):
    if hours <= 40:
        return[hours, False]
    else:
        othour = hours - 40
        print('****',othour)
        return[othour, True]


def payCalc(hours, wage):
    if hours[0]:
        othours = hours[0]
        otpay = othours * (wage * 1.5)
        regularpay = 40 * wage
        return otpay, regularpay, othours
    else:
        return None


def main():
    hours, wage = data()
    if payCalc(timeCalc(hours), wage) is None:
        print('      Payroll Information      ')
        print('Pay Rate: $%.2f' % wage)
        print('Regular Hours: %.0f' % hours)
        print('Overtime Hours: 0')
        print('Regular pay: $%.2f' % (hours * wage))
        print('Overtime pay: $0.00')
        print('')
        print('Total pay: $%.2f' % (hours * wage))
    else:
        otpay, regularpay, othours = payCalc(timeCalc(hours), wage,)
        print('      Payroll Information      ')
        print('Pay Rate: $%.2f' % wage)
        print('Regular Hours: %.0f' % (hours - othours))
        print('Overtime Hours: %.0f' % othours)
        print('Regular pay: $%.2f' % (hours * wage))
        print('Overtime pay: $%.2f' % otpay)
        print('Total pay: $%.2f' % (regularpay + otpay))


if __name__ == '__main__':
    main()


python3 zh574.py
What is your payrate? 11.5
How many hours were worked? 48
**** 8.0
**** 8.0
      Payroll Information
Pay Rate: $11.50
Regular Hours: 40
Overtime Hours: 8
Regular pay: $552.00
Overtime pay: $138.00
Total pay: $598.00
localhost:python hjzaaa$