Brandon Peavy
Brandon Peavy

Reputation: 19

Payroll Calculator in python

I'm writing a payroll calculator for school in python 3. The user input starts by asking for your name or "0" to quit the program. whenever I enter "0" at the start the program closes as it should, but if I enter it after calculating a users pay it prints (end of report and the previous payroll information). I can't figure out how to get it to stop printing the payroll information after you end it. This is what I have so far.

This is the code: One Stop Shop Payroll Calculator

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)

This is how it shows up when you run it:

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

code

code being executed

Upvotes: 1

Views: 37505

Answers (3)

brian
brian

Reputation: 21

Here's what I ended up coming up with

    print("Payroll Calculator")
    user = input("Enter Employee Name or '0' to Quit: ")
    end = "0"
    while user!=end:
        Hours = float(input("Please Enter Hours worked: "))
        Rate = float(input("Please Enter Rate of Pay: $"))
        if Hours < 40:
            GrossPay = round(Rate*Hours, 2)
            print("Employee Name: ", user)
            print("Gross Pay: $", GrossPay)
        else:
            RegularPay = Rate*40
            OverTime = Hours-40
            OverTimeRate = Rate*1.5
            OverTimePay = round(OverTimeRate*OverTime,2)
            GrossPay = round(RegularPay+OverTimePay, 2)
            print("Employee Name: ", user)
            print("Gross Pay: $", GrossPay)
            print("(Overtime pay: $",OverTimePay,")")
        user = input("Enter Next Employee or type '0' to Exit: ")
    else:
        print("Exiting program....")

Upvotes: 2

Jonathan
Jonathan

Reputation: 21

I made one as well. Here's mine in 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)))

And that would output for example

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)

if under 40 hours ^^^, or,

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

if above 40 hours.

Upvotes: 1

NicheQuiche
NicheQuiche

Reputation: 82

So I think I have got it. I have copied most of your code and it all works well. Let me explain my changes so if this helps you know why it works.

First off I changed the method of defining when to end. So rather than it being: end = "0", I set it to: end = False, the while loop then runs whilst "end == False".

Then I slightly changed the first line in the while loop so it says: if user == "0":

This checks the input "user" and if its = to 0, it runs the next line.

I then left the print, but added a break. Thats the key here. Without the break, it just continues the loop after the "end report". But with the break in place, it exits out of the loop after printing "end report".

I also had to change the line: "if hours > 40" to "if hours <= 40" as otherwise typing in 40 wouldn't work

So now the code looks like this:

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)

God I wish I could copy code with the whitespace indent. Would save me having to edit this haha

Upvotes: 1

Related Questions