我的代码仅在最后打印“净工资”,但是我需要它来打印整个输出。它还在括号内打印输出,我要修复该括号。我只需要一个打印语句,但是它不起作用。我还希望该程序输出在所有大写字母中输入的员工姓名,后跟“工资信息”一词,但它也不起作用。
employee_name = input("Enter employee's name: ")
hours_worked = float(input("Enter number of hours worked in a week: "))
pay_rate = float(input("Enter hourly pay rate: "))
federal_tax = float(input("Enter federal tax withholding rate (ex. 0.12): "))
state_tax = float(input("Enter state tax withholding rate (ex. 0.06): "))
gross_pay = hours_worked * pay_rate
federal_withholding = gross_pay * federal_tax
state_withholding = gross_pay * state_tax
net_pay = gross_pay - (state_withholding + federal_withholding)
message = employee_name.upper, "PAY INFORMATION"
message = "Hours Worked: ", hours_worked
message = "Pay Rate:","$"+str(round(pay_rate,2))
message = "Gross Pay:","$"+str(round(gross_pay,2))
message = "Deductions:"
message = " Federal Withholding (11.0%):", format(federal_withholding,".2f")
message = " State Withholding (7.0%):","$"+str(round(state_withholding,2))
message = " Total Deduction: ", "$" + str(federal_withholding + state_withholding)
message = "Net Pay:", "$"+ str(round(net_pay,2))
print(message)
答案 0 :(得分:1)
一个打印声明
有一个Pythonic way of creating multi-lines string,它比您正在做的事情和以前在其他答案(特别是较旧的答案)中建议的内容更干净。请参见下面的代码:
employee_name = input("Enter employee's name: ")
hours_worked = float(input("Enter number of hours worked in a week: "))
pay_rate = float(input("Enter hourly pay rate: "))
federal_tax = float(input("Enter federal tax withholding rate (ex. 0.12): "))
state_tax = float(input("Enter state tax withholding rate (ex. 0.06): "))
gross_pay = hours_worked * pay_rate
federal_withholding = gross_pay * federal_tax
state_withholding = gross_pay * state_tax
net_pay = gross_pay - (state_withholding + federal_withholding)
message = f"""
{employee_name.upper()}, PAY INFORMATION \n
Hours Worked : {hours_worked} \n
Pay Rate : ${str(round(pay_rate,2))} \n
Gross Pay : ${str(round(gross_pay,2))} \n
Deductions : \n
\t Federal Withholding (11.0%) : ${format(federal_withholding,".2f")} \n
\t State Withholding (7.0%) : ${str(round(state_withholding,2))} \n
\t Total Deduction : ${str(federal_withholding + state_withholding)} \n
Net Pay : ${str(round(net_pay,2))}
"""
print(message)
注意,upper()
是一种方法,因此您需要()
才能起作用。
答案 1 :(得分:1)
假设您使用的是Python 3.6+,则可以按如下所示使用f字符串:
enum SortPlacement
{
Try_To_Keep_Same_Order_Of_X_and_Y = 0,
Place_X_Before_Y_Ascending_Order = -1, // or any negative number
Place_X_After_Y_Descending_Order = 1, // or any positive number
}
请注意,您没有正确使用public SortPlacement Compare(int x, int y)
{
// OPTION 1
if (x < y)
return SortPlacement.Place_X_Before_Y_Ascending_Order;
...
// OPTION 2
if (x < y)
return SortPlacement.Place_X_After_Y_Descending_Order;
...
// OPTION 3
return SortPlacement.Try_To_Keep_Same_Order_Of_X_and_Y;
}
方法message = f"""
{employee_name.upper()}, PAY INFORMATION
Hours Worked : {hours_worked}
Pay Rate : ${str(round(pay_rate,2))}
Gross Pay : ${str(round(gross_pay,2))}
Deductions :
Federal Withholding (11.0%) : ${federal_withholding:.2f}
State Withholding (7.0%) : ${round(state_withholding,2)}
Total Deduction : ${federal_withholding + state_withholding}
Net Pay : ${round(net_pay,2)}
"""
print(message)
。这是一种方法,因此应将其命名为str
。
输入/输出
upper
答案 2 :(得分:0)
已编辑以显示换行符“ \ n”,并将变量写为字符串。
您面临的问题是消息变量的重新分配。需要添加\ n来告诉python在另一行继续该字符串。您的浮动变量需要转换为字符串。
employee_name = input("Enter employee's name: ")
hours_worked = float(input("Enter number of hours worked in a week: "))
pay_rate = float(input("Enter hourly pay rate: "))
federal_tax = float(input("Enter federal tax withholding rate (ex. 0.12): "))
state_tax = float(input("Enter state tax withholding rate (ex. 0.06): "))
gross_pay = hours_worked * pay_rate
federal_withholding = gross_pay * federal_tax
state_withholding = gross_pay * state_tax
net_pay = gross_pay - (state_withholding + federal_withholding)
message = (
employee_name.upper() + " PAY INFORMATION" + "\n"
"Hours Worked: "+ str(hours_worked) + "\n"
"Pay Rate:"+"$"+str(round(pay_rate,2)) + "\n"
"Gross Pay:"+"$"+str(round(gross_pay,2)) + "\n"
"Deductions:" + "\n"
" Federal Withholding (11.0%):"+ str(format(federal_withholding,".2f")) + "\n"
" State Withholding (7.0%):"+"$"+str(round(state_withholding,2)) + "\n"
" Total Deduction: "+ "$" + str(federal_withholding + state_withholding) + "\n"
"Net Pay:"+ "$"+ str(round(net_pay,2))
)
print(message)