gpt4 book ai didi

python - 一份打印声明

转载 作者:行者123 更新时间:2023-12-01 00:32:18 25 4
gpt4 key购买 nike

我的代码仅在最后打印“净工资”,但我需要它来打印整个输出。它还在括号内打印输出,我想修复它。我只需要一份打印声明,但它不起作用。我还希望程序输出以全部大写形式输入的员工姓名,后跟“PAY INFORMATION”一词,但它也不起作用。

    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)

最佳答案

One single print statement

有一个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() 是一种方法,因此您需要() 才能使其工作。

关于python - 一份打印声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58090002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com