python if和else语句计算员工工资

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17959255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:36:15  来源:igfitidea点击:

python if and else statements calculating employees pay

python

提问by user2612625

I'm having a little trouble with this assignment its about calculating the employees pay it goes like Write a Python program that prompts the user for an hourly rate and a number of hours worked and computes a pay amount. Any hours worked over 40 are paid at time and a half (1.5 times the normal hourly rate). Write a version of the program using the if/else my code so far is this

我在这个任务中遇到了一些问题,它是关于计算员工工资的,就像编写一个 Python 程序,提示用户输入小时费率和工作小时数并计算工资金额。任何超过 40 小时的工作都按时间支付一半(正常小时工资的 1.5 倍)。使用 if/else 编写程序的一个版本,到目前为止我的代码是这样的

hours = int(input('how many hours did you work? '))
rate = 1.50
rate = (hours/2)+hours*(rate+1.5)
if hours<40:
 print("you earn",rate)

回答by Blender

Some hints:

一些提示:

  • You need to prompt the user for their hourly rate as well.
  • It's rate * 1.5, not rate + 1.5. That rate is only applied to hours past40, so for the first 40 hours, you apply the regular rate:

    if hours <= 40:
        total = hours * rate
    else:
        total = 40 * rate + (hours - 40) * (1.5 * rate)
    
  • 您还需要提示用户输入他们的小时费率。
  • rate * 1.5,不是rate + 1.5。这个速度只适用于小时过去的40,所以对于第一个40小时,应用常规的速度:

    if hours <= 40:
        total = hours * rate
    else:
        total = 40 * rate + (hours - 40) * (1.5 * rate)
    

回答by Aaron Cronin

You could use:

你可以使用:

pay = rate * min(hours, 40)
if hours > 40:
    pay += rate * 1.5 * (hours - 40)

To adjust the pay calculations depending on the number of hours worked.

根据工作小时数调整工资计算。

You should probably become familiar with this resource.

您可能应该熟悉此资源

回答by James Holderness

If you need to input both the hoursand ratefrom the user, you can do so like this:

如果您需要输入用户的小时数费率,您可以这样做:

hours = int(input('how many hours did you work? '))
rate = int(input('what is your hourly rate? '))

Then once you have those variables, you can start by calculating the overtime.

然后一旦你有了这些变量,你就可以从计算加班开始。

if hours > 40:
  # anything over 40 hours earns the overtime rate
  overtimeRate = 1.5 * rate
  overtime = (hours-40) * overtimeRate
  # the remaining 40 hours will earn the regular rate
  hours = 40
else:
  # if you didn't work over 40 hours, there is no overtime
  overtime = 0

Then calculate the regular hours:

然后计算正常时间:

regular = hours * rate

Your total pay is regular + overtime.

你的总工资是regular + overtime

回答by John La Rooy

print("you earn", (hours + max(hours - 40, 0) * 0.5) * rate)

or for a golfed version

或高尔夫球版

print("you earn", (hours*3-min(40,hours))*rate/2)