Novice looking for Python assistance

Hello everyone, I have stumbled across this wonderful site while trying to find assistance with Python. Being virtual in my class is making this very difficult for me. The problem I am working on has three parts to it.

PART 1

Build a payroll solution prompting for employee name, hours worked, and pay rate. Calculate the gross pay as hours worked * pay rate. Calculate the taxes (straight 20%) to be deducted. Then display the employee name, hours worked, gross pay taxes, and net pay you calculated.

Here is my code for Part 1

#Prompt the user to enter their name.
name = input("What Is Your Name?")

#Prompt the user to enter their hours worked.
hours = float(input("How Many Hours Did You Work This Week?"))

#Prompt the user to enter their hourly pay rate.
rate = float(input("What Is Your Current Hourly Pay Rate?"))

#Prompt the user to enter the federal tax withholding rate.
federal_tax_rate = float(input("What Is Your Current Federal Tax Withholding Rate?"))

#Display user name.
print ("Employee Name:",name)

#Display users working hours.
print ("Hours Worked:",hours)

#Display users rate of pay.
print ("Pay Rate:$",rate)

#Display users Gross Pay.
print ("Gross Pay:$",rate * hours)

#Display users deductions.
print ("Deductions:")

#Calculate and print the Federal tax withholding.
print (" Federal Withholding (",federal_tax_rate*100,"%): ${:.2f}".format(rate * hours * federal_tax_rate,'.2f'))

#Calculate and print the total deduction.
print (" Total Deduction: ${:.2f}".format(rate * hours * federal_tax_rate))

#Calculate and print the net pay.
print ("Net Pay: $ {:.2f}".format(rate * hours - rate * hours * federal_tax_rate))

PART 2

Modify the program from PART 1 in two ways:

  1. Embedding the code in a loop, so multiple employees can be calculated without restarting the program. Terminate the loop when no employee name is entered.
  2. modify the tax calculation so that anyone
  3. making less than $385 pays $0 tax,
  4. makes < $576 pays 15% tax,
  5. makes less than $769 pays 20% tax
  6. and all others pay 25% tax.

Here is my code for Part 2 where I am stuck. I think I am actually combining part three in this as well. I am just so confused currently.

while True:
    # Prompt the user to enter their name.
    name = input("What Is Your Name? ")
    if name == "":  # if name is blank exit the loop
        break

    # Prompt the user to enter their hours worked.
    hours = float(input("How Many Hours Did You Work This Week? "))

    # Prompt the user to enter their hourly pay rate.
    rate = float(input("What Is Your Current Hourly Pay Rate? "))

    # Display user name.
    print("Employee Name:", name)

    # Display users working hours.
    print("Hours Worked:", hours)

    # Display users rate of pay.
    print("Pay Rate:$", rate)

    grossPay = rate * hours
    # Display users Gross Pay.

    if grossPay < 385:  # less than 385 0 tax
        return 0;
    elif grossPay >= 385 or grossPay < 576:  # less than 576 15% tax
        return (grossPay * 15) / 100  # 15% tax
    elif grossPay >= 576 or grossPay < 769:  # less than 769 20% tax
        return (grossPay * 20) / 100  # 20% tax
    else:  # all other 25% tax
        return (grossPay * 25) / 100  # 25% tax
        # print("Gross Pay:$", grossPay)
        print("Gross Pay:$", rate * hours)

    # Calculate and print the total deduction.
    print(" Total Deduction: ${:.2f}".format(federal_tax_withholding + tax))

    # Calculate and print the net pay.
    print("Net Pay: $ {:.2f}".format(netPay))
    print()

PART 3

Modify the program from PART 2 moving the tax calculation into a function to be called TaxCalc.

1 Like

Currently, part 2 should throw an error, since you are returning from outside a function. As this is an assignment, we can’t give code/solutions here on the forums, but we can hint. Is there any particular thing you’re struggling with?

2 Likes

Thank you for your response. This is what I had intended to implement for Part 3:

# function that calculate the tax
def TaxCalc(grossPay):
    if grossPay < 385:  # less than 385 0 tax
        return 0;
    elif grossPay >= 385 or grossPay < 576:  # less than 576 15% tax
        return (grossPay * 15) / 100  # 15% tax
    elif grossPay >= 576 or grossPay < 769:  # less than 769 20% tax
        return (grossPay * 20) / 100  # 20% tax
    else:  # all other 25% tax
        return (grossPay * 25) / 100  # 25% tax

I thought I would have been able to use the code without the def statement in part two. I have not been able to get the code to calculate the individual tax amount once I changed it from a set tax amount in Part 1.

1 Like

In addition to the comment above which mentioned an issue perhaps the docs on return could shed a little light for you-

1 Like