python中的工资单计算器

时间:2016-07-19 22:11:06

标签: python-3.x pycharm

我正在为python 3中的学校写一个工资单计算器。用户输入首先询问你的名字或“0”退出程序。每当我在开始时输入“0”,程序就会按原样关闭,但如果我在计算用户支付后输入它,则打印(报告结束和之前的工资单信息)。结束后,我无法弄清楚如何让它停止打印工资单信息。这是我到目前为止所做的。

这是代码:     一站式工资计算器

user = str
end = "0"
hours = round(40,2)
print("One Stop Shop Payroll Calculator")
while user != end:
    print()
    user = input("Please enter your name or type '0' to quit: ")
if user == end:
    print("End of Report")
else:
    hours = (float(input("Please enter hours worked: ", )))
    payrate =(float(input("Please enter your payrate: $", )))
if hours < 40:
    print("Employee's name: ", user)
    print("Overtime hours: 0")
    print("Overtime Pay: $0.00")
    regularpay = round(hours * payrate, 2)
    print("Gross Pay: $", regularpay)
elif hours > 40:
    overtimehours = round(hours - 40.00,2)
    print("Overtime hours: ", overtimehours)
    print("Employee's name: ", user)
    regularpay = round(hours * payrate,2)
    overtimerate = round(payrate * 1.5, 2)
    overtimepay = round(overtimehours * overtimerate)
    grosspay = round(regularpay+overtimepay,2)
    print("Regular Pay: $", regularpay)
    print("Overtime Pay: $",overtimepay)
    print("Gross Pay: $", grosspay)

这是运行时显示的方式:

One Stop Shop Payroll Calculator

Please enter your name or type '0' to quit: Brandon
Please enter hours worked: 50
Please enter your payrate: $10
Overtime hours:  10.0
Employee's name:  Brandon
Regular Pay: $ 500.0
Overtime Pay: $ 150
Gross Pay: $ 650.0

Please enter your name or type '0' to quit: Brandon
Please enter hours worked: 30
Please enter your payrate: $10
Employee's name:  Brandon
Overtime hours: 0
Overtime Pay: $0.00
Gross Pay: $ 300.0

Please enter your name or type '0' to quit: 0
End of Report
Employee's name:  0
Overtime hours: 0
Overtime Pay: $0.00
Gross Pay: $ 300.0

Process finished with exit code 0

code

code being executed

3 个答案:

答案 0 :(得分:0)

所以我想我已经得到了它。我复制了大部分代码,一切运作良好。让我解释一下我的更改,如果这有助于您了解它的工作原理。

首先,我改变了定义何时结束的方法。因此,而不是它:end =“0”,我将其设置为:end = False,while循环然后运行,而“end == False”。

然后我稍微改变了while循环中的第一行,所以它说:if user ==“0”:

这将检查输入“user”,如果其=为0,则运行下一行。

然后我离开了印刷品,但又加了一个休息时间。这是关键所在。如果没有中断,它只会在“结束报告”之后继续循环。但随着中断的到位,它会在打印出“结束报告”之后退出循环。

我还必须更改该行:“如果小时> 40”到“如果小时&lt; = 40”,否则输入40将无效

所以现在代码看起来像这样:

user = str
end = False
hours = round(40,2)
print("One Stop Shop Payroll Calculator")
while end == False:
  user = input("\nPlease enter your name or type '0' to quit: ")
  if user == "0":
     print("End of Report")
     break
  else:
     hours = (float(input("Please enter hours worked: ", )))
     payrate =(float(input("Please enter your payrate: $", )))
  if hours <= 40:
     print("Employee's name: ", user)
     print("Overtime hours: 0")
     print("Overtime Pay: $0.00")
     regularpay = round(hours * payrate, 2)
     print("Gross Pay: $", regularpay)
  elif hours > 40:
     overtimehours = round(hours - 40.00,2)
     print("Overtime hours: ", overtimehours)
     print("Employee's name: ", user)
     regularpay = round(hours * payrate,2)
     overtimerate = round(payrate * 1.5, 2)
     overtimepay = round(overtimehours * overtimerate)
     grosspay = round(regularpay+overtimepay,2)
     print("Regular Pay: $", regularpay)
     print("Overtime Pay: $",overtimepay)
     print("Gross Pay: $", grosspay)

上帝,我希望我能用空格缩进来复制代码。能节省我不得不编辑这个哈哈

答案 1 :(得分:0)

我也做了一个。这是我的Python 3x。

print("Welcome to PayCalc!\n")
wage = float(input("How much do you make per hour?\n"))
hours = float(input("How many hours for the week?\n"))
def as_currency(amount):
    if amount >= 0:
        return '${:,.2f}'.format(amount)
    else:
        return '-${:,.2f}'.format(-amount)
if hours <= 40:
    weekincome = wage*hours
    monthincome = weekincome*4
    print("It has been calculated that if you work {} hours at a rate of {}, you should make a total of {}/week ({}/month)".format(int(round(hours)),as_currency(wage),as_currency(weekincome),as_currency(monthincome)))
else:
    regularpay = wage*40
    overtimehours = hours - 40
    overtimerate = wage*1.5
    overtimeincome = (overtimehours * overtimerate)
    print("Regular pay: {}/wk + your overtime rate of {}/hr".format(as_currency(regularpay),as_currency(overtimerate)))
    print("Hours of overtime: {}".format(int(round(overtimehours))))
    print("Total overtime income: {}".format(as_currency(overtimeincome)))
    weekincome = (40*wage) + overtimeincome
    #if worked overtime every week
    monthincome = weekincome*4
    overtimeonce = weekincome + (regularpay*3)
    print("It has been calculated that you should make a total of {}/week with overtime ({}/month) if worked {} hours every week.\nIf worked {} hours during one week and 40 hours/wk every other week, you'd make {} for the month".format(as_currency(weekincome),as_currency(monthincome),int(round(hours)),int(round(hours)),as_currency(overtimeonce)))

那将输出例如

Welcome to PayCalc!

How much do you make per hour?
19.12
How many hours for the week?
40
It has been calculated that if you work 40 hours at a rate of $19.12, you should make a total of $764.80/week ($3,059.20/month)

如果不到40小时^^^,或者

Welcome to PayCalc!

How much do you make per hour?
19.12
How many hours for the week?
60
Regular pay: $764.80/wk + your overtime rate of $28.68/hr
Hours of overtime: 20
Total overtime income: $573.60
It has been calculated that you should make a total of $1,338.40/week with overtime ($5,353.60/month) if worked 60 hours every week.
If worked 60 hours during one week and 40 hours/wk every other week, you'd make $3,632.80 for the month

如果超过40小时。

答案 2 :(得分:0)

这就是我最终想到的

    print("Payroll Calculator")
    user = input("Enter Employee Name or '0' to Quit: ")
    end = "0"
    while user!=end:
        Hours = float(input("Please Enter Hours worked: "))
        Rate = float(input("Please Enter Rate of Pay: $"))
        if Hours < 40:
            GrossPay = round(Rate*Hours, 2)
            print("Employee Name: ", user)
            print("Gross Pay: $", GrossPay)
        else:
            RegularPay = Rate*40
            OverTime = Hours-40
            OverTimeRate = Rate*1.5
            OverTimePay = round(OverTimeRate*OverTime,2)
            GrossPay = round(RegularPay+OverTimePay, 2)
            print("Employee Name: ", user)
            print("Gross Pay: $", GrossPay)
            print("(Overtime pay: $",OverTimePay,")")
        user = input("Enter Next Employee or type '0' to Exit: ")
    else:
        print("Exiting program....")