简体   繁体   English

Python 3 - 实现一个函数来计算小时工资和工作小时数

[英]Python 3 - Implement a function to compute hourly wage and number of hours worked

I'm working on a question where I need to create a program that takes two arguments: an hourly wage and the number of hours an employee worked.我正在研究一个问题,我需要创建一个需要两个参数的程序:小时工资和员工工作的小时数。 Any hours beyond 40 is overtime and should be paid at a rate of 1.5 times more.任何超过 40 小时的时间都是加班费,应支付1.5倍以上的工资。

So far, I've come up with the following:到目前为止,我想出了以下几点:

def wage(hourly, hours)
if hours > 40
#hours over 40 earns overtime rate

It should come out something along the lines of:它应该出现以下内容:

   >>> wage(10, 10) 
   100

   >>> wage(10, 35) 
   50

   >> wage(10,45)
   475

The function should output and calculate the values based off how the program is written.该函数应根据程序的编写方式输出和计算值。 I'm just not sure how to implement the proper iteration with the user-defined function in order to output the proper values.我只是不确定如何使用用户定义的函数实现正确的迭代以输出正确的值。

As reference, heres the original question:作为参考,这里是原始问题:

Implement function wage() that takes two arguments: an hourly wage and the number of hours an employee worked in the last week.实现带有两个参数的函数工资():小时工资和员工上周工作的小时数。 Your function should compute and return the employee's pay.您的函数应该计算并返回员工的工资。 Any hours worked beyond 40 is overtime and should be paid at 1.5 times the regular hourly wage.任何超过 40 小时的工作时间都是加班费,应按正常小时工资的 1.5 倍支付。 Make sure to add a Docstring to tell the user how the program works.确保添加一个 Docstring 来告诉用户程序是如何工作的。

Just multiply hourly against hours, compute overtime and add that to salary.只需将小时乘以小时,计算加班时间并将其添加到工资中。 Since you already added regular hourly rate, you add 0.5 * overtime.由于您已经添加了常规小时费率,因此您添加了 0.5 * 加班费。

def wage(hourly, hours):
    salary = hourly * hours
    if hours > 40:
        overtime = 40 - hours
        salary += 0.5 * overtime
    return salary

Even if I strongly believe that you should really do it yourself, since you should know this thing if you are going to use Python, here is the answer:即使我坚信你真的应该自己做,因为如果你打算使用 Python,你应该知道这件事,这里是答案:

def wage(hourly,hours):
        if hours > 40:
                payment = 40 * hourly  # Standard Payment until 40 Hours
                payment = payment + hourly * (hours-40) * 1.5  # + the rest which has more rate
                return payment
        else:
                return hours * hourly  # Otherwise Normal Payment

You need to find the amount of hours that the person worked that are over 40. These have the 1.5 rate.您需要找到该人工作超过 40 小时的数量。这些小时数为 1.5。 So, the first 40 hours have standard rate.因此,前 40 小时有标准费率。 That's why you need to subtract the first 40 hours from the rest, in the first case.这就是为什么在第一种情况下,您需要从其余时间中减去前 40 小时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM