pythonclasshierarchy

Learning Python, need help making an object with user-given values that will display as a list


I'm wrapping up an assignment that's due today, but I had an early shift this morning and my brain is turning into goo. What I'm doing here is creating a superclass (Employee) and a subclass (ProductionWorkers).

I think I made the classes themselves okay, but the last thing the program needs to have is an object of the ProductionWorker class that lets the user enter their own data for each attribute.

Then finally, it needs to display the user-given data. I assume it needs to be a list.

I've tried a few different things now. You can see some remnants of a list(?) near the bottom of the code. Here's what I have so far, if someone could drop a solution I would really appreciate it:

# This program is designed to create a superclass 'Employee' and
# a subclass 'ProductionWorkers'.

# Establish the main function.

def main():

    # Introduce the user to the program.
    print('Welcome to the employee information page.')
    print('Let\'s get your information entered into the system.')
     
    # Create the Employee superclass.
    class Employee:
        
        # Establish the attributes of Employee.
        def __init__(self, name, number):
            self.__name = name
            self.__number = number
         
         # The user needs to be able to set the values for these
         # attributes.
        def set_name(self, name):
            self.__name = name
                
        def set_number(self, number):
            self.__number = number
             
        # The program will need to return these values as well.
        def get_name(self):
            return self.__name
            
        def get_number(self):
            return self.__number
            
    # Establish the subclass ProductionWorkers.
    class ProductionWorkers(Employee):
       
       # We first need to initialize the preexisting attributes of
       # this subclass.
        def __init__(self, name, number):
           
            Employee.__init__(self, name, number)
           
           # Add the attributes unique to this subclass.
            self.shift_number = shift_number
           
            self.hourly_pay_rate = hourly_pay_rate
           
        # Allow the user to set the values of these new attributes.
        def set_shift_number(self, shift_number):
            self.__shift_number = shift_number
            
        def set_hourly_pay_rate(self, hourly_pay_rate):
            self.__hourly_pay_rate = hourly_pay_rate
            
        # Again, we want the program to display these values.
        def get_shift_number(self):
            return self.__shift_number
        
        def get_hourly_pay_rate(self):
            return self.__hourly_pay_rate
 
     # Now to make the objects that allow the user to enter their
     # own unique data.
    name = input(print('Enter your first name and last inital. '))
    number = input(print('Enter your employee number. '))
    shift_number = input(print('Do you work a day shift (1) or a night shift (2)? '))
    hourly_pay_rate = input(print('Enter your pay rate (per hour).'))
 
    employee_info = [name, number, shift_number, hourly_pay_rate]

    # Display the user's information.
    return str(ProductionWorkers)

# Call the main function.
main()

I got really lost towards the end here, that's why it makes no sense.


Solution

  • I don't know what your error is, but you cant have a print() inside of an input().
    Also you are calling the ProductionWorkers() class, but you aren't giving the required arguments for the class.

    Here is what I think you were trying to achieve:

    name = input('Enter your first name and last initial. ')
    number = input('Enter your employee number. ')
    shift_number = input('Do you work a day shift(1), or a night shift(2). ')
    hourly_pay_rate = input('Enter your pay rate (per hour). ')
    
    employee = ProductionWorkers(name, number, shift_number, hourly_pay_rate)
    employee_info = [name, number, shift_number, hourly_pay_rate]
    
    # I don't know why you have 'return str(ProductionWorkers)', dm me if you
    # need any help (discord: j3sterishere)
    
    print('Employee Information:')
    print('Name:', employee.get_name())
    print('Employee Number:', employee.get_number())
    print('Shift Number:', employee.get_shift_number())
    print('Hourly Pay Rate:', employee.get_hourly_pay_rate())
    # or
    for item in employee_info:
        print(item)
    

    Please dm me if you need any additional help (discord: j3sterishere)