Answered step by step
Verified Expert Solution
Link Copied!

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:/Python/Python312/test.py", line 130, in
EmployeeInfo()
File "C:/Python/Python312/test.py", line 103, 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
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:
outfile = open("C:\\employee.dat", "wb")
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)
another = input("Enter another employee? (y/n): ")
if another.lower()=="n":
break
except ValueError:
print("Pay rate and hours worked must be numbers.")
except IOError:
print("Error opening or writing to employee.dat.")
except pickle.PickleError:
print("Error using pickle module.")
finally:
outfile.close()
def ShowData():
try:
with open("C:\\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 the file until end of file
while True:
try:
# Load an Employee object from the 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 the functions
EmployeeInfo()
ShowData()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions