Good evening everyone,
I am once again posting a question. My school project is done. And I appreciate all the help you gave me. The following code is a working program. The only thing I have absolutely no real good idea to do is calculate the Overtime Rate with my variables. I can't seen to find out. Here is my code:

#ASCII Images and Text
raw_input("""

 _______  __  .__   __.      ___       __      
|   ____||  | |  \ |  |     /   \     |  |     
|  |__   |  | |   \|  |    /  ^  \    |  |     
|   __|  |  | |  . `  |   /  /_\  \   |  |     
|  |     |  | |  |\   |  /  _____  \  |  `----.
|__|     |__| |__| \__| /__/     \__\ |_______|
                                               
.______   .______        ______          __   _______   ______ .___________.
|   _  \  |   _  \      /  __  \        |  | |   ____| /      ||           |
|  |_)  | |  |_)  |    |  |  |  |       |  | |  |__   |  ,----'`---|  |----`
|   ___/  |      /     |  |  |  | .--.  |  | |   __|  |  |         |  |     
|  |      |  |\  \----.|  `--'  | |  `--'  | |  |____ |  `----.    |  |     
| _|      | _| `._____| \______/   \______/  |_______| \______|    |__|     
 	                                           
 	                                       
 	                                        

             ,----------------,              ,---------,
        ,-----------------------,          ,"        ,"|
      ,"                      ,"|        ,"        ,"  |
     +-----------------------+  |      ,"        ,"    |
     |  .-----------------.  |  |     +---------+      |
     |  |                 |  |  |     | -==----'|      |
     |  | I Love Python!  |  |  |     |         |      |
     |  | And you can TOO!|  |  |/----|`---=    |      |
     |  |                 |  |  |   ,/|==== ooo |      ;
     |  |                 |  |  |  // |(((( [33]|    ,"
     |  `-----------------'  |," .;'| |((((     |  ,"
     +-----------------------+  ;;  | |         |,"
        /_)______________(_/  //'   | +---------+
   ___________________________/___  `,
  /  oooooooooooooooo  .o.  oooo /,   \,"-----------
 / ==ooooooooooooooo==.o.  ooo= //   ,`\--{)B     ,"
/_==__==========__==_ooo__ooo=_/'   /___________,"
`-----------------------------'
____,"
`-----------------------------'


 """)

#Starts Program
raw_input("\nHit ENTER to start Payroll Program.")

#Mainline
hours=[]
hWork="1"

#Intial Loop
while True:
    eName=raw_input("\nPlease enter the employees' first and last name. ")
    hWork=raw_input("How many hours did they work this week? ")
    intWork=int(hWork)
#Condition 1
    if intWork < 1 or intWork > 60:
        print "Employees' can't work more than 60 hours or less than 1 hour!"
        continue
#Condition 2
    else:
        pRate=raw_input("What is their hourly rate? ")
        intRate=int(pRate)
#Condition 3
        if intRate < 6 or intRate > 20:
            print "Employees' wages can't be  or greater than $20.00 lower than $6.00!"
#Condition 4
        else:
            Salary=intWork*intRate
            strmoney=str(Salary)
            hours.append(strmoney + "\n")
            ePass = ""
            ePass = raw_input("Type DONE when finished with employees' information. ")
            if ePass  == "DONE":
                
#I/O Exceptions
                try:
                    hoursFile=open("PAY.txt", "w")
                    hoursFile.writelines(hours)
                    hoursFile.close()
                except(IOError):
                    print "Error writing hours! "
                    quit()
                try:
                    hoursFile=open("PAY.txt", "r")
                    hoursList=hoursFile.readlines()
                    hoursFile.close()
                    hoursList.sort()
                except(IOError):
                    print "Error writing names! "
                    quit()
                break


#Mainline 1
def employee():
    global Name
    global Rate
    global Work

#Mainline 2
def Lowest(hours):
    hours.sort()
    lowestPay = hours[0]
    return lowestPay

#Mainline 3
def Highest(hours):
    hours.sort()
    highestPay=hours[len(hours)-1]
    return highestPay

#Mainline 4
def Average(hours):
    total=0
    for hour in hours:
        total += int(hour)
    avgPay = total / len(hours)
    return avgPay

#Prints Salary Totals
print Lowest(hours)
print Highest(hours)
print Average(hours)

I would appreciate any feedback you guys can give me. Like I said I am not quite sure how to calculate overtime with my variables (to be honest I don't really know much about Python). Thank you so very much for your feedback. This is a great site. And I will continue to use this site in the future when it comes to trouble shooting.

Dani AI

Generated

A few focused points that will plug directly into the program and solve the overtime problem while also fixing a couple of hidden bugs in the posted code.

Overtime rule (common approach): treat the first 40 hours as regular pay and any hours above 40 as overtime at 1.5x (time-and-a-half). Use floating-point arithmetic so cents are kept. Concretely: regular = min(hours, 40) rate; overtime = max(0, hours - 40) rate * 1.5; total = regular + overtime. Keep numeric values in lists (not strings) so min/max/sum work correctly.

Example helper (drop this into your code and call it with numeric hours and rate):

def compute_pay(hours, rate, ot_threshold=40.0, ot_multiplier=1.5):
    hours = float(hours)
    rate = float(rate)
    regular_hours = min(hours, ot_threshold)
    overtime_hours = max(0.0, hours - ot_threshold)
    total = regular_hours * rate + overtime_hours * rate * ot_multiplier
    return round(total, 2)

Practical tips: as noted, make sure to use continue or otherwise re-prompt after invalid input. As asked, protect conversions with try/except and accept floats (use float() for rates and hours). Store numeric salaries (e.g., salaries.append(total)) and later use min(salaries), max(salaries), and sum(salaries)/len(salaries) for averages (watch out for integer division in old Python 2—use floats or from __future__ import division). When writing to file, format values with two decimals (for example, "%.2f\n" % total) so the saved output is human-friendly and sorts/compares correctly.

Recommended Answers

All 2 Replies

I notice that your condition 3 is missing a continue statement...

To answer your question however, you should check to see if intWork is greater than 40 and if it is, calculate the overtime based on the number of hours over 40 that the employee worked.

When someone enters pay rate or hours, what happens if they accidentally enter a letter or a float instead (pay rate is $9.99)? Or does the problem let you assume that only integers are entered,

pRate=raw_input("What is their hourly rate? ")
intRate=int(pRate)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.