Home > OS >  User inputs in class function and endless loops
User inputs in class function and endless loops

Time:03-31

I have the following task to complete:

Create:

  1. A class which creates a generic staff member. Staff members have a date of birth ‘dd/mm/yyyy’, first name, last name, and department. A person named Jon Snow, who was born on the 18 July 1990 and works in the accounts department. Jon Snow printed to screen

  2. A class which creates a generic staff member, allowing for initialisation on entry, but otherwise the same as the requirements above. A prompt that allows the user to enter a staff member’s details to create a new staff object. The staff member details printed to screen on completion of entry.

  3. A class which creates a teaching staff member, which inherits the basic staff attributes from the class created above, but also includes discipline and license number attributes. A prompt that allows the user to enter the teacher staff member’s details to create a new teaching staff object. The teaching staff member details printed to screen on completion of entry.

  4. The program should operate in an endless loop (with 0 as an exit key) so that once staff details are entered, the user can enter details for the additional staff members. The user should get to choose whether they enter a staff member or a teaching staff member All staff members generated should be printed into a txt file.

I have managed to program the following so far, however I receive the following error - TypeError: __init__() missing 3 required positional arguments: 'first', 'last', and 'dep'.

I am seeking assistance with my code below and how I can modify it to fulfill the tasks listed above. I am stuck on task 2. I can ask for the user inputs but I can't seem to display the input correctly.

Any help with the above tasks will be greatly appreciated!

class staff:
    dob =  '18/07/1990'
    first = 'John'
    last = 'Snow'
    dep = 'Accounts'

#Prints the staff member's name - John Snow
JS = staff()
print(JS.first, JS.last)

class staff2:
    def __init__(dob, first, last, dep):
        self.dob = dob
        self.first = first
        self.last = last
        self.dep = dep
        
class staff2(staff):
    def __init__(dob, first, name, last, dep):
        super().__init__(dob, first, name)
        self.dep = dep

#collect user input
dobInput = input("Enter staff member's date of birth: ")
firstInput = input("Enter staff member's first name: ")
lastInput = input("Enter staff member's last name: ")
depInput = input("Enter staff member's department: ")

GS = staff2()
print(GS.dob, GS.first, GS.last, GS.dep)

CodePudding user response:

Something like this will make your code work, you need to put the self in the init and you had the same class defined twice. You still need to customized by your requirements.

class staff:
    def __init__(self, dob, first, last): #here dep is not required you can default everything
        self.dob = dob
        self.first = first
        self.last = last
        self.dep = 'default'
        
class staff2(staff):
    def __init__(self, dob, first, last, dep):  # here dep is required
        super().__init__(dob, first, last)
        self.dep = dep
        

#collect user input
dobInput = input("Enter staff member's date of birth: ")
firstInput = input("Enter staff member's first name: ")
lastInput = input("Enter staff member's last name: ")
depInput = input("Enter staff member's department: ")

GS = staff2(dobInput, firstInput, lastInput, depInput)

CodePudding user response:

You are not assigning the received input to your class.

Modify GS = staff2 into:

GS = staff2(dobInput, firstInput, lastInput, depInput)

Now you can use GB.dob to access the property.

  • Related