-2

Have a program where i have made 2 classes linked to eachother one parent one child to store string inputs the parent takes 4 inputs and the child takes 6 i need to find a way to write the attributes that are the inputs taken from user into a .txt file using the class object made from the user inputs.

#define parent class
class Staff:
    def __init__(self, fname, lname, dob, department):
    self.fname = fname
    self.lname = lname
    self.dob = dob
    self.department = department

#define print function to print the parent class object
def printsf(self):
    print(self.fname, self.lname, self.dob, self.department)

#define child class
class TeachingStaff(Staff):
    def __init__(self, fname, lname, dob, department, discipline, licenseNumber):
    super().__init__(fname, lname, dob, department)
    self.discipline = discipline
    self.licenseNumber = licenseNumber

#define print function to print the child class object
def printtsf(self):
    print(self.fname, self.lname, self.dob, self.department, self.discipline, self.licenseNumber)

staffDetails = open("staffDetails.txt", "a")  # open file for appending
teachingDetails = open("teachingDetails.txt", "a")  # open file for appending

loopdobInput = input("Enter the date of birth in the format DD/MM/YYYY:")
loopfnameInput = input("Enter first name:")
looplnameInput = input("Enter last name:")
loopdepartmentInput = input("Enter department name:")
loopdecisionInput = input("Is the staff member a teacher (Y/N?")
if loopdecisionInput == "y" or loopdecisionInput == "Y":
    loopdisiplineInput = input("Enter discipline:")
    looplicenceInput = input("Enter licence number:")
    loopteachingInput = TeachingStaff(loopdobInput, loopfnameInput, looplnameInput,          loopdepartmentInput,
    loopdisiplineInput, looplicenceInput)
    loopteachingstr = str(loopteachingInput)
    teachingDetails.write(loopteachingstr)
else:
    loopstaffInput = Staff(loopdobInput, loopfnameInput, looplnameInput, loopdepartmentInput)
    loopstaffstr = str(loopstaffInput)
    staffDetails.write(loopstaffstr)

staffDetails.close()
teachingDetails.close()

In my attempts i tryed to convert the object to str and write to file but this just printed the following for the teachingDetails.txt which is the one i was testing with <main.TeachingStaff object at 0x000001EAE84A1390>. What id like is for it to print the 4 string inputs to text file for staffDetails.txt or 6 string inputs for teachingDetails.txt but unsure how to tackle this.

  • Then, when you define [at least] `__repr__`, you can get rid of the print methods – buran May 05 '23 at 07:57
  • Maybe ill have a try. The print function I made seems to work I just need to find a way to convert to write to file instead of print to screen – Phillip Handfield May 05 '23 at 07:59

1 Answers1

0

Found a solution by adjusting my function to fit the requirements of the assignment by just adding a file= to the text file variable in the function, and it worked exactly how I wanted: writetsf(self): print(self.fname, self.lname, self.dob, self.department, self.discipline, self.licenseNumber, file=teachingDetails)