Am I able to read or write to a file with this code or what is stumping me. The following is what I am suppose to do.

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.

lsthours = list()
eName = "start" # initialize to something to start the loop
while eName:
    eName = raw_input("\nPlease enter the employees' first and last name. ")
    if not eName:
        break #loop will exit also when blank name is inserted
    hWork = raw_input("How many hours did they work this week? ")
    hoursWork = int(hWork)
    if hoursWork < 1 or hoursWork > 60:
        print "Employees' can't work less than 1 hour or more than 60 hours!"
        continue #skip

    pRate = raw_input("What is their hourly rate? ")
    payRate = int(pRate)
    if payRate < 6 or payRate > 20:
        print "Employees' wages can't be lower than $6.00 or greater than $20.00!"
        continue #skip
    if hoursWork <= 40:
        grossPay = hoursWork * payRate
        grossPay = ((hoursWork - 40) * (payRate * 1.5)) + (40 * payRate)
    print grossPay
    print lsthours
    ePass = raw_input("Type DONE when finished with employees' information. ")
    if ePass.upper() == "DONE":

try:#handling exceptions
    fileNames=open("PAY.txt", "w")#opens the file to read
    fileNames.writelines(map(int, lsthours)
    fileNames.close()#closes the file
except(IOError):#handling exceptions
    print "Error writing NAMES file."

    fileNames=open("PAY.txt", "r")#opens the file to read
    fileNames.close()#closes the file
    lstfileNames.sort()#sorts the list
    print "Error reading NAMES file."
##    fileSortedNames=open("SORTEDPAY.txt","w")
##    fileSortedNames.writelines(lstfileNames)
##    fileSortedNames.close()#closes the file
##    print "Error writing SORTEDNAMES file."
##    quit()
##    fileNames=open("SORTEDPAY.txt", "r")#opens the file to read
##    lstfileNames=fileNames.readlines()#changes the file to a list
##    fileNames.close()
##    print "Error reading file."
##    quit()
for name in lstfileNames:#for loop prints the list down the page
    print name

Recommended Answers

All 3 Replies

The 31. line lacks a closing brace ")".
The 22. line appends a list to a list. Is this what you want? So 31. line makes an int from a one member list ....
44. There is no quit function
You name lstfileNames a list, that contains gross payments. This is confusing.
No need to put braces around exception name if you have only one.
3. is redundant. can be while True.
34.,43. You write PAY.TXT and if exception is raised, you write NAMES file.
Read PEP-8. Please no hungarian notation....

I am a student trying to learn this so go easy. everything works but I am not able to write to the file PAY.txt

I am going easy.
If I try to run your code, the first syntax error is on line 32, because - as I said before - line 31 lacks closing ")".
If I correct this by adding a ), then the next runtime error comes on line 31 because - as I said before - lsthours is a list of lists and cannot be converted to int. I do not know why do you want to convert it to int, writelines expects a list of strings.

In the end two lines has changed:
fileNames.writelines(map(lambda x: str(x)+"\n", lsthours))

Be a part of the DaniWeb community

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