Question: I keep getting these errors. I know it has to do with my employeeinfo function but Im unsure on how to fix it . Nothing

I keep getting these errors. I know it has to do with my employeeinfo function but Im unsure on how to fix it. Nothing seems to be changing this and I cant seem to stop it at all. How do I fix this? The solutions provided havnt been helpful. Its always the same errors.
Error opening or writing to employee.dat.
Traceback (most recent call last):
File "C:/Python/Python312/test.py", line 132, in
EmployeeInfo()
File "C:/Python/Python312/test.py", line 105, in EmployeeInfo
outfile.close()
UnboundLocalError: cannot access local variable 'outfile' where it is not associated with a value
import pickle
class Employee:
def __init__(self, Fname="", Lname="", social="", rate=0, hrs_worked=0):
self.Fname = Fname.upper()
self.Lname = Lname.upper()
self.social = social
self.rate = rate
self.hrs_worked = hrs_worked
#Getters & setters
def get_Fname(self):
return self.Fname
def set_Fname(self, Fname):
self.Fname = Fname.upper()
def get_Lname(self):
return self.Lname
def set_Lname(self, Lname):
self.Lname = Lname.upper()
def get_social(self):
return self.social
def set_social(self, social):
self.social = social
def get_rate(self):
return self.rate
def set_rate(self, rate):
self.rate = rate
def get_hrs_worked(self):
return self.hrs_worked
def set_hrs_worked(self, hrs_worked):
self.hrs_worked = hrs_worked
def get_gross(self):
if self.hrs_worked >40:
return (40* self.rate)+((self.hrs_worked -40)*(self.rate *1.5))
else:
return self.hrs_worked * self.rate
def __str__(self):
Fname = self.Fname +""+ self.Lname
last_four_digits = self.social[-4:]
gross = self.get_gross()
return f"{Fname}({last_four_digits}) ${gross:.2f}"
def EmployeeInfo():
try:
#opens file
outfile = open("C:\\Users\\CC4 STUDENT\\Desktop\\CST301_LEC_151\\Activity 5.3 Classes and ObjectsActivity 5.3\\employee.dat", "wb")
#Asks user for info
while True:
Fname = input("Enter an employee first name: ")
Lname = input("Enter the last name the employee: ")
social = input("Enter the social security number (it must be 9 digits): ")
while len(social)!=9:
print("The social security number must be 9 digits in length.")
print("Please enter again ")
social = input("Enter the social security number (it must be 9 digits): ")
rate = input("Enter the pay rate of the employee: ")
hrs_worked = input("Enter the hours worked by the employee: ")
try:
rate = float(rate)
hrs_worked = float(hrs_worked)
employee = Employee(Fname, Lname, social, rate, hrs_worked)
pickle.dump(employee, outfile)
except ValueError:
print("Pay rate and hours worked must be numbers.")
continue
#Asks if you want to enter another employee
another = input("Enter another employee? (y/n): ")
if another.lower()=="n":
break
#exception handling
except IOError:
print("Error opening or writing to employee.dat.")
except pickle.PickleError:
print("Error using pickle module.")
finally:
outfile.close()
def ShowData():
try:
#open file to read
with open("C:\\Users\\CC4 STUDENT\\Desktop\\CST301_LEC_151\\Activity 5.3 Classes and ObjectsActivity 5.3\\employee.dat", "rb") as file:
# Print headings
print("{:<20}{:<15}{:<10}{:<10}{:<10}".format(" Employee Name", "ID", "Hours Worked", "Pay Rate", "Gross Pay"))
# Loop through file until end
while True:
try:
# Load an employee object from file
obj = pickle.load(file)
print("{:<20}{:<15}{:<10}{:<10}{:<10}".format(
obj.get_Fname()+""+ obj.get_Lname(),
obj.get_social()[-4:],
obj.get_hrs_worked(),
obj.get_rate(),
obj.get_gross()
))
except EOFError:
break
except IOError:
print("The file could not be found.")
# Call functions
EmployeeInfo()
ShowData()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!