charleywm
charleywm

Reputation: 1

New at coding and struggling with organizing an if-else statement

I am new to coding and writing a code that calculates if an employee has worked overtime hours or not, and then creating an invoice based on their hours worked. Below is the if-else statement section of my code, and I can tell it could be organized better. I have an idea of what I want to do but am struggling to implement it into my code. I want to use one main print statement under the if-else statement to clean things up. To do this I want to introduce two new variables, regMSG and overtimeMSG. regMSG should include the part of the invoice with all things not overtime, and overtimeMSG should include all things overtime. My main issue creating these new variables is being able to have them print in the correct format and also include the necessary variables to create them.

This is what I wish the print statement at the end to look like (roughly, and I know the indentation might be weird):

        print("\nInvoice","\nResource:",(employee),"\t", "Average Weekly Hours:",\
              format(averageHours, ".2f"),
        '\n',
        "\nTotal Billable Hours:",format(totalHours,',.2f'),'\trate: $'\
              +format(rate, '.2f'),
        overtimeMSG,
        regMSG,
        '\nAmount Due: $'+format(invoiceAmount, ',.2f'))    

(if no overtime was worked, I don't want that portion of the message to show up.)

What the code looks like now:

if float(totalHours)>MIN_HOURS_WORKED:
        OTrate = round((rate * OT_RAISE),2)
        OThours = round((totalHours - MIN_HOURS_WORKED),2)
        OTtotal = round((OThours*OTrate),2)

        regHours = round((totalHours - OThours),2)
        regTotal = round((regHours*rate),2)

        invoiceAmount = round((regTotal + OTtotal),2)

        print("\n",(employee)," has worked ",\
              format(OThours, ',.2f')," hours of overtime.",sep='')
        
        print("\nInvoice","\nResource:",(employee),"\t", "Average Weekly Hours:",\
              format(averageHours, ".2f"),
        '\n',
        "\nTotal Billable Hours:",format(totalHours,',.2f'),'\trate: $'\
              +format(rate, '.2f'),
        "\nOvertime Hours:",format(OThours,',.2f'),'@ $' +format(OTrate, ',.2f'),'= $'\
              +format(OTtotal,',.2f'),
        "\nRegular Hours:",format(regHours,',.2f'),'@ $' +format(rate,',.2f'),'= $'\
              +format(regTotal,',.2f'),
        '\nAmount Due: $'+format(invoiceAmount, ',.2f'))    
    else:
        regHours = totalHours
        regTotal = round((regHours*rate),2)


        print("\n",(employee)," has worked no overtime", sep='')
              
        print("\nInvoice","\nResource:",(employee),"\t", "Average Weekly Hours:",\
              format(averageHours, ".2f"),
        '\n',
        "\nTotal Billable Hours:",format(regHours,',.2f'),'\trate: $'\
              +format(rate, '.2f'),
        "\nRegular Hours:",format(regHours,',.2f'),'@ $' +format(rate,',.2f'),'= $'\
              +format(regTotal,',.2f'),
        '\nAmount Due: $'+format(regTotal, ',.2f'))

Thank you in advance for anyone willing to help.

Upvotes: 0

Views: 59

Answers (2)

JonSG
JonSG

Reputation: 13087

Rather than maintaining two longer statements with repeated bits, I might look at building up the statement conditionally.

I'm note sure some of the values you use or where averageHours comes from, so I just added some placeholder values.

MIN_HOURS_WORKED = 40

employee_name = "Someone"
averageHours = -1
totalHours = 55
base_rate = 15.0
overtime_rate = base_rate * 1.5

regular_hours = min(totalHours, MIN_HOURS_WORKED)
regular_total = regular_hours * base_rate
overtime_hours = max(totalHours - regular_hours, 0)
overtime_total = overtime_hours * overtime_rate
invoiceAmount = regular_total + overtime_total

## -------------------
## store each "line" we want to print in a list
## -------------------
statement = []
## -------------------

if overtime_hours:
    statement.append(f"{employee_name} has worked {overtime_hours} hours of overtime.")
else:
    statement.append(f"{employee_name} has worked no overtime.")

statement.append("Invoice")
statement.append(f"Resource: {employee_name}\tAverage Weekly Hours: {averageHours}")
statement.append(f"Total Billable Hours: {totalHours}, base rate ${base_rate}")

if overtime_hours:
    statement.append(f"Overtime Hours: {overtime_hours} at ${overtime_rate}")

statement.append(f"Regular Hours: {regular_hours} at ${base_rate}")
statement.append(f"Amount Due: ${ round(invoiceAmount, 2) }")

## -------------------
## emit the statement by joining our list with new lines
## -------------------
print("\n".join(statement))
## -------------------

That should result in something like:

Someone has worked 15 hours of overtime.
Invoice
Resource: Someone       Average Weekly Hours: -1
Total Billable Hours: 55, base rate $15.0
Overtime Hours: 15 at $22.5
Regular Hours: 40 at $15.0
Amount Due: $937.5

Upvotes: 0

alvrm
alvrm

Reputation: 363

The issue seems to be with the way you use format().

'{:.2f}'.format(34.5562356754786456346753)
output: 34.56
'{:.5f}'.format(34.5562356754786456346753)
output: 34.55624

Upvotes: 0

Related Questions