Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I keep getting a few errors. I know it has to do with my employeeinfo function but Im unsure on how to fix it .
I keep getting a few errors. I know it has to do with my employeeinfo function but Im unsure on how to fix it
Traceback most recent call last:
File C:PythonPythontestpy line in
EmployeeInfo
File C:PythonPythontestpy line in EmployeeInfo
outfile.close
UnboundLocalError: cannot access local variable 'outfile' where it is not associated with a value
import pickle
class Employee:
def initself Fname Lname social rate hrsworked:
self.Fname Fname.upper
self.Lname Lname.upper
self.social social
self.rate rate
self.hrsworked hrsworked
def getFnameself:
return self.Fname
def setFnameself Fname:
self.Fname Fname.upper
def getLnameself:
return self.Lname
def setLnameself Lname:
self.Lname Lname.upper
def getsocialself:
return self.social
def setsocialself social:
self.social social
def getrateself:
return self.rate
def setrateself rate:
self.rate rate
def gethrsworkedself:
return self.hrsworked
def sethrsworkedself hrsworked:
self.hrsworked hrsworked
def getgrossself:
if self.hrsworked :
return self.rateselfhrsworked selfrate
else:
return self.hrsworked self.rate
def strself:
Fname self.Fname self.Lname
lastfourdigits self.social:
gross self.getgross
return fFnamelastfourdigits $gross:f
def EmployeeInfo:
try:
outfile openC:employeedat", wb
while True:
Fname inputEnter an employee first name:
Lname inputEnter the last name the employee:
social inputEnter the social security number it must be digits:
while lensocial:
printThe social security number must be digits in length."
printPlease enter again
social inputEnter the social security number it must be digits:
rate inputEnter the pay rate of the employee:
hrsworked inputEnter the hours worked by the employee:
try:
rate floatrate
hrsworked floathrsworked
employee EmployeeFname Lname, social, rate, hrsworked
pickle.dumpemployee outfile
another inputEnter another employee? yn:
if another.lowern:
break
except ValueError:
printPay rate and hours worked must be numbers."
except IOError:
printError opening or writing to employee.dat."
except pickle.PickleError:
printError using pickle module."
finally:
outfile.close
def ShowData:
try:
with openC:employeedat", rb as file:
# Print headings
print:::::format Employee Name", ID "Hours Worked", "Pay Rate", "Gross Pay"
# Loop through the file until end of file
while True:
try:
# Load an Employee object from the file
obj pickle.loadfile
print:::::format
obj.getFname obj.getLname
obj.getsocial:
obj.gethrsworked
obj.getrate
obj.getgross
except EOFError:
break
except IOError:
printThe file could not be found."
# Call the functions
EmployeeInfo
ShowData
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started