我正在用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
所以我想我明白了。我已经复制了您的大多数代码,并且一切正常。让我解释一下我的更改,以便如果可以帮助您知道它为什么起作用。
首先,我更改了定义何时结束的方法。因此,不是将其设置为:end =“ 0”,而是将其设置为:end = False,然后while循环运行,而“ end == False”。
然后我稍微更改了while循环的第一行,所以它说:if user ==“ 0”:
这将检查输入“用户”,如果其=等于0,则运行下一行。
然后我离开了印刷品,但稍作休息。这就是关键。没有中断,它只是在“结束报告”之后继续循环。但是,如果有适当的中断,它会在打印“结束报告”后退出循环。
我还不得不将行:“如果小时数> 40”更改为“如果小时数<= 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)
上帝,我希望我可以使用空格缩进来复制代码。将省去我编辑此哈哈的麻烦
我也做了一个。这是我的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小时。