Python的Payroll程序使用多个函数和返回函数

时间:2014-03-18 22:12:05

标签: python

好的我需要这个程序的帮助。 这是作业

编写一份工资单计划,为40岁以上的任何人支付一半时间 小时。除了main之外,这应该有3个功能。

  1. 第一个功能询问用户总共有多少小时 工作和工资率,并将此信息返回主要。这些 值必须经过验证。工作时间必须至少为8小时,不得多 支付率不能低于7.00美元或超过50.00美元。
  2. 第二个功能计算常规时间和加班时间 小时并将此信息返回给main。一个人可能会减少工作 超过40小时所以你必须考虑到这种情况。
  3. 第三个函数计算常规工资,(常规工时) 倍付率);加班费,(加班时间加班费) 费率)和总工资,并将此信息返回主要。
  4. Main将在屏幕上显示此信息,如: 以下样本。 (必须传递和返回值。)
  5. 输出应如下所示:

                 Payroll Information
    Pay rate                           $10.00
    Regular Hours                       40
    Overtime Hours                      20
    Regular pay                        $400.00
    Overtime pay                       $300.00
    Total Pay                          $700.00
    

    这是我到目前为止所拥有的

    def work_info():
        hw = input('Enter the amount of hours worked between 8 and 56')
        while hw < 8 or hw > 86:
            print('Enter the amount of hours in range')
        pr = input('Enter the your current payrate between $7 and $50')
        while pr < 7 or pr > 50:
            print('Enter a payrate in range')
    
    def reg_ovt_hours():
        if hw < 40:
            reg_hours = hw and ovt_hours = 0
        else reg_hours > 40
            ovt_hours = hw - 40
    
        return hw
    
    def reg_pay():
        reg_pay = reg_hours * pr
        ovt_pay = ovt_hours * reg_pay * 1.5
        tot_pay = reg_pay + ovt_pay
        return pr, reg_hours, ovt_hours
    
    def main():
        print('Payroll Information')
        print ('Pay rate') pr
        print reg_hours
        print ovt_hours
        print reg_pay
        print ovt_pay
        print tot_pay
    
    main() 
    

    请告诉我如何让这项工作正常

2 个答案:

答案 0 :(得分:0)

这一行错了:

else reg_hours > 40

应该是

elif reg_hours > 40:

或者

else:

你错过了一个冒号,你不能拥有else的条件,因为它涵盖了一切,好吧,“其他”。

reg_hours == 40会怎样?

答案 1 :(得分:0)

您必须了解定义变量的范围。我建议在python中查看关于变量范围的教程。

我建议您查看这个问题:Short Description of the Scoping Rules?

例如,在work_info中,您定义了hwpr,但目前它是唯一定义它们的地方。因此,稍后当您尝试使用这些变量时,您将收到错误,因为python解释器并不知道它们是什么。

您还需要将else reg_hours > 40更改为else:elif reg_hours > 40: