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
else:
grossPay = ((hoursWork - 40) * (payRate * 1.5)) + (40 * payRate)
lsthours.append([grossPay])
print grossPay
print lsthours
ePass = raw_input("Type DONE when finished with employees' information. ")
if ePass.upper() == "DONE":
break
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."
quit()
try:
fileNames=open("PAY.txt", "r")#opens the file to read
lstfileNames=fileNames.readlines()
fileNames.close()#closes the file
lstfileNames.sort()#sorts the list
except(IOError):
print "Error reading NAMES file."
quit()
##try:
## fileSortedNames=open("SORTEDPAY.txt","w")
## fileSortedNames.writelines(lstfileNames)
## fileSortedNames.close()#closes the file
##except(IOError):
## print "Error writing SORTEDNAMES file."
## quit()
##try:
## fileNames=open("SORTEDPAY.txt", "r")#opens the file to read
## lstfileNames=fileNames.readlines()#changes the file to a list
## fileNames.close()
##except(IOError):
## print "Error reading file."
## quit()
##print
##print
##
for name in lstfileNames:#for loop prints the list down the page
print name