1

My code only prints the 'net pay' at the end, but I need it to print the entire output. It also prints the output inside a parenthesis, which I want to fix. I need to have only one print statement but it is not working. I also want the program to output the employee name entered in all caps followed by the words "PAY INFORMATION" but it is not working either.

    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)
Phill
  • 29
  • 4

3 Answers3

1

One single print statement

There's a Pythonic way of creating multi-lines string, which is more cleaner than what you are doing and what is previously suggested in other answer (specifically the older one). Please see code below:

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)

And NOTE that, upper() is a method so you need the () for that to work.

CodeRed
  • 905
  • 1
  • 6
  • 24
1

Assuming you are using Python 3.6+, you can use an f-string as follows:

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)

Notice that you were not using correctly the str method upper. This is a method, so you should call it employee_name.upper().

Input/Output

Enter employee's name: Miguel
Enter number of hours worked in a week: 8
Enter hourly pay rate: 50
Enter federal tax withholding rate (ex. 0.12): .1
Enter state tax withholding rate (ex. 0.06): .15
MIGUEL PAY INFORMATION
Hours Worked: 8.0
Pay Rate: $50.0
Gross Pay: $ 400.0
Deductions:
   Federal Withholding (11.0%): 40.00
   State Withholding (7.0%): $60.0
   Total Deduction: $100.0
Net Pay: $300.0
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
0

edited to show line breaks "\n" and writing variables as strings.
the problem you are facing is reassignment of your message variable. the \n needs to be added to tell python to continue the string on another line. your floating variables need to be converted to strings.

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)
tbrk
  • 156
  • 8