pythonclassparameterssubclasssuperclass

Could you help me make my method in my subclass work properly?


class CommissionPaid(Employee):
    def __init__(self, name, dept, base_rate, sales):
        super().__init__(name, dept)
        self.__base_rate = base_rate
        self.__sales = sales
        
    def get_base_rate(self):
        return self.__base_rate

    def get_sales(self):
        return self.__sales

    def get_employee_type(self):
        return 'Commission Paid'
    
    def set_base_rate(self, base_rate):
        self.__base_rate = base_rate

    def set_sales(self, sales):
        self.__sales = sales
        
    def pay(self, base_rate = 0.0, sales = 0.0):
        if sales > 10000:
            pay = sales * .02 + base_rate
            return pay
        elif sales >= 5000 and sales <=10000:
            pay = sales * .01 + base_rate
            return pay
        elif sales < 5000:
            return base_rate
    
    def __str__(self):
        return f'self.get_employee_type() \t {super().__str__()} \t {self.pay(base_rate, sales)}'

###################################################################################################### MAIN PROGRAM

from employees import CommissionPaid, HourlyPaid

def main():
    employee1 = CommissionPaid('Fname1 Lname1', 'Marketing', 500, 30000)
    employee2 = CommissionPaid('Fname2 Lname2', 'Sales', 1000, 8000)
    employee3 = HourlyPaid('Fname3 Lname3', 'Accounting', 55, 20.5)
    employee4 = HourlyPaid('Fname4 Lname4', 'Finance', 35, 30)
    employee_list = [employee1, employee2, employee3, employee4]
    return employee_list

def print_employee_list(employee_list):
    print('Employee Type \t Employee Name \t Department    \t Weekly Pay')
    print('_____________ \t _____________ \t ___________ \t _____________')
    print(employee_list[0].__str__())
    print(employee_list[1].__str__())
    print(employee_list[2].__str__())
    print(employee_list[3].__str__())
    print('\n',total_pay())

def total_pay(employee_list):
    total_pay = 0
    for index in employee_list:
        total_pay += employee_list[index].pay()
    return f'Total Weekly Pay: ${total_pay}'

employee_list = main()
print(employee_list[0])
print_employee_list(employee_list)

**When I try to run the above program that instantiates the subclasses that I created, I get this error. I feel this might be something simple to fix, but I need help **

Traceback (most recent call last):
  File "F:/Classes/Python/Python II/COSC 1437/Programming Assignments/2/assignment2_Carissa_Sims.py", line 32, in <module>
    print(employee_list[0])
  File "F:/Classes/Python/Python II/COSC 1437/Programming Assignments/2\employees.py", line 63, in __str__
    return f'self.get_employee_type() \t {super().__str__()} \t {self.pay(base_rate, sales)}'
NameError: name 'base_rate' is not defined

I tried initializing the parameter in the .pay() method but it still did not fix the problem.


Solution

  • Your function CommissionPaid.__str__ is error

    class CommissionPaid(Employee):
       ....
       def __str__(self):
            return f'self.get_employee_type() \t {super().__str__()} \t {self.pay(base_rate, sales)}'
    

    It should change from self.pay(base_rate, sales) -> self.pay(self.__base_rate, self.__sales)

    class CommissionPaid(Employee):
       ....
    
       def __str__(self):
            return f'self.get_employee_type() \t {super().__str__()} \t {self.pay(self.__base_rate, self.__sales)}'