Hey guys and gals I am really struggling with this program. I have been working on it for about a week now and every time I think I am getting somewhere it seems like it flops and I get frustrated and start over. I would appreciate it if anyone could help me out. I think I need to start with some function but not really sure. We learned about functions last week and I just don't get it. So again any help would be greatly appreciated.


Create a Python program that meets the following requirements:


• Ask the user to enter payroll information for the company. Set up a loop that continues to ask for information until they enter “DONE”. For each employee ask three questions:
name (first & last)
hours worked this week (only allow 1 through 60)
hourly wage (only allow 6.00 through 20.00)
VALIDATE the hours worked and the hourly wage, and make sure a name is entered.

• Calculate each employee’s pay, and write it out to a sequential file. Be sure to include file I/O error handling logic.
Include only the weekly pay
Weekly pay is calculated:
For (1-40 hours) it is hourly rate * hours worked
For (41-60 hours) it is (hours worked – 40) * (hourly rate * 1.5)
+ hourly rate * 40

• After all the employees are entered, read in the sequential file into a list named PAY for the weekly pay of each employee. Sort the list. Now print the lowest, highest, and average weekly pay for the week.

Please post your code.

That is the problem I keep erasing and am frustrated at where to start

this is one version of what I have. but I can't get it to do what the instructions ask.. I have only been doing PYTHON for about 8 weeks and not very familiar with it so please explain so I can understand.

def Lowest(hoursList):
    PAYList.sort()
    lowestPay = hoursList[0]
    return lowestPay

def Highest(hoursList):
    hoursList.sort()
    highestPay=hoursList[len(hoursList)-1]
    return highestPay

def Average(hoursList):
    total=0
    for hour in hoursList:
        total += hour
    avgPay = total / len(hoursList)
    return avgPay


hours=[]
hWork=1
while True:
    employee=raw_input("\nPlease enter the employees' first and last name. ")
    hWork=int(raw_input("How many hours did they work this week? "))
    if hWork < 1 or hWork > 60:
        print "Employees' can't work less than 1 hour or more than 60 hours!"
        continue
    else:
        pRate=int(raw_input("What is their hourly rate? "))
        if pRate < 6 or pRate > 20:
            print "Employees' wages can't be lower than $6.00 or greater than $20.00!"
        else:
            hours.append(hWork)
            empDone = ""
            empDone = raw_input("Type DONE when finished with employees' information. ")
            if empDone == "DONE":
                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()
                    
                modHours = hoursList
                for hour in hoursList:
                    print hour

You need to change the integer back to string in order to write lines.
In line 38, change

hoursFile.writelines(hours)

to

hoursFile.writelines(map(str, hours))

After line 55, add a "break".

for hour in hoursList:
                    print hour
                break

How do i get it so when a user types in DONE on line 34 to convert different inputs to UPPERCASE.

Change
if empDone == "DONE":
To
if empDone.upper() == "DONE":

Am I calculating the weekly pay part right. I am so sorry for all the questions

also how do i get all this to write out in sequential order

Am I calculating the weekly pay part right. I am so sorry for all the questions

I don't think you have post the part of the code on the weekly pay.

This is my updated code. Not sure if I got the weekly pay portion in the right spot.

def Lowest(hoursList):
    PAYList.sort()
    lowestPay = hoursList[0]
    return lowestPay

def Highest(hoursList):
    hoursList.sort()
    highestPay=hoursList[len(hoursList)-1]
    return highestPay

def Average(hoursList):
    total=0
    for hour in hoursList:
        total += hour
    avgPay = total / len(hoursList)
    return avgPay

employees=[]
hours=[]
rate=[]
PAY=[]
hWork=1
while True:
    employee=raw_input("\nPlease enter the employees' first and last name. ")
    employees.append(employee)
    hWork=int(raw_input("How many hours did they work this week? "))
    hours.append(hWork)
    if hWork < 1 or hWork > 60:
        print "Employees' can't work less than 1 hour or more than 60 hours!"
        continue
    else:
        pRate=int(raw_input("What is their hourly rate? "))
        rate.append(pRate)
        if pRate < 6 or pRate > 20:
            print "Employees' wages can't be lower than $6.00 or greater than $20.00!"
    if hWork <=40:
        gPay=hWork*pRate
    else:
        gPay=(hWorked-40)*(pRate*1.5)+pRate*40

        else:
            hours.append(hWork\n)
            empDone = ""
            empDone = raw_input("Type DONE when finished with employees' information. ")
            if empDone.upper() == "DONE":
                try:
                    hoursFile=open("PAYLIST.txt", "w")
                    hoursFile.writelines(map(str,hours))
                    hoursFile.close()
                except(IOError):
                    print "Error writing hours! "
                    quit()
                    
                try:
                    hoursFile=open("PAYLIST.txt", "r")
                    hoursList=hoursFile.readlines()
                    hoursFile.close()
                    hoursList.sort()
                except(IOError):
                    print "Error writing names! "
                    quit()
                    
                modHours = hoursList
                for hour in hoursList:
                    print hour
                break

This is my update code. not sure if my weekly pay part is in the right spot, or if I even entered it in the right way.

def Lowest(hoursList):
    PAYList.sort()
    lowestPay = hoursList[0]
    return lowestPay

def Highest(hoursList):
    hoursList.sort()
    highestPay=hoursList[len(hoursList)-1]
    return highestPay

def Average(hoursList):
    total=0
    for hour in hoursList:
        total += hour
    avgPay = total / len(hoursList)
    return avgPay

employees=[]
hours=[]
rate=[]
PAY=[]
hWork=1
while True:
    employee=raw_input("\nPlease enter the employees' first and last name. ")
    employees.append(employee)
    hWork=int(raw_input("How many hours did they work this week? "))
    hours.append(hWork)
    if hWork < 1 or hWork > 60:
        print "Employees' can't work less than 1 hour or more than 60 hours!"
        continue
    else:
        pRate=int(raw_input("What is their hourly rate? "))
        rate.append(pRate)
        if pRate < 6 or pRate > 20:
            print "Employees' wages can't be lower than $6.00 or greater than $20.00!"
        if hWork <=40:
            gPay=hWork*pRate
        else:
            gPay=(hWorked-40)*(pRate*1.5)+pRate*40
  
        else:
            hours.append(hWork\n)
            empDone = ""
            empDone = raw_input("Type DONE when finished with employees' information. ")
            if empDone.upper() == "DONE":
                try:
                    hoursFile=open("PAYLIST.txt", "w")
                    hoursFile.writelines(map(str,hours))
                    hoursFile.close()
                except(IOError):
                    print "Error writing hours! "
                    quit()
                    
                try:
                    hoursFile=open("PAYLIST.txt", "r")
                    hoursList=hoursFile.readlines()
                    hoursFile.close()
                    hoursList.sort()
                except(IOError):
                    print "Error writing names! "
                    quit()
                    
                modHours = hoursList
                for hour in hoursList:
                    print hour
                break

You need to move 36-39 to be in the same indentation level as the previous lines.
Also, I think you did not read the questions correctly, you should write the gPay
you computed to PAY.txt rather than the hours worked.

I am getting an error on line 41. Also what about the gPay and PAY.txt can you show me
please

@henryford you are getting desperate, which always makes bad code. Please take a minute to breathe, get a drink of water or caffienated, not booze :) and walk around the room. I'll wait.

OK. The right way to do this is to break off a piece and do just the piece. Get it right. Now add a piece, etc.

I suggest you start by asking for the user name until you see 'DONE', then write it to a file. Check that the file is correct. Probably you have a list of names, right?

Now, add the question about hours. Verify the hours before you put them into your data structure. If you are using a list, you have trouble: Parallel lists are so... FORTRAN... when you can use a map/dictionary: users = {}...users[name]=hours... for k,v in users.items():... Make sure you can write it out

Now add the wage. users[name] = [hours,wage]... .

Now do the rest of the work. At every step, you have something and it works (albeit not fully). This is worth a lot...

[edit] Oh. And if you aren't using some form of source control, then you need to put that on your 'do very soon' list: Source control allows you to fiddle around without fear of losing something. Consider mercurial (hg) or svn or ... anything.

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.