0

Trying to figure out what this wants. Below is the error im getting in IDE

This is the AttributeError and Traceback error that I am getting in IDE

Traceback (most recent call last):
  File "10.py", line 77, in <module>
    main()
  File "10.py", line 21, in <module>
    display_list(workers)
  File "10.py", line 72, in <module>
    person.show_pay()
  File "worker.py", line 74, in show_pay
    premium_rate = self.__pay_rate
builtins.AttributeError: 'Hourlyworker' object has no attribute '_Hourlyworker__pay_rate'

here is the 10.py program code

import worker

def main():
    #Create a list to store employee and volunteer objects
    workers = make_list()

    #Display the objects in the list
    print ('\n\nHere are the workers, their ID numbers, city of residence, shift, and pay.')
    print ('--------------------------------------------------------------')

    display_list(workers)

def make_list():
    #Create an empty list
    worker_list = []

    #Create a volunteer object
    print ('Create a volunteer worker object:')
    name = input('\nWhat is the employee name? ')
    id_number = input("What is the employee's number? ")
    city = input("What is the employee's city? ")
    volunteerworker = worker.Person(name, id_number, city)
    worker_list.append(volunteerworker)

    #Create 3 hourly worker objects
    print ('Create 3 hourly worker objects:')
    for hourlyworkers in range(1,4):
        name = input('\nWhat is the employee number ' + str(hourlyworkers + 1) + "'s name? ")
        id_number = input("What is the employee's number? ")
        city = input("What is the employee's city? ")
        shift = input("What is the employee's shift? ")
        #validate the shift entered is 1, 2, or 3
        while shift != '1' and shift != '2' and shift != '3':
            print( 'ERROR: the shift can only be 1, 2, or 3.')
            shift = input('Please enter the correct shift: ')

        pay_rate = float(input("What is the employee's hourly pay rate? "))
        hourlyworker = worker.Hourlyworker(name, id_number, city, shift, pay_rate)
        worker_list.append(hourlyworker)

    #create a salary worker object
    print ('Create a volunteer worker object')
    name = input("\nWhat is the salaried employee's name? ")
    id_number = input("What is the employee's number? ")
    city = input("WHat is the employee's city? ")
    pay_rate = float(input("What is the employee's annual salary? "))
    salaryworker = worker.SalaryWorker(name, id_number, city, pay_rate)
    worker_list.append(salaryworker)

    #append invalid object to list
    worker_list.append('\nThis is an invalid object')

    return worker_list

#This function accpets an object as an argument, and calls its show_employee
#and show_pay methods

def display_list(worker_list):
    for person in worker_list:
        if isinstance(person, worker.Person):
            person.show_employee()
            person.show_pay()
            print()
        else:
            print('That is not a valid employee type!')

main()

This is the worker.py program code

PREMIUM2 = .05
PREMIUM3 = .10
PAY_PERIODS = 26

class Person:

    #The __init__ method initialized the attributes
    def __init__(self, name, id_number, city):
        self.__name = name
        self.__id_number = id_number
        self.__city = city

    #This method accepts an argument for the person's name
    def set_name(self, name):
        self.__name = name

    #This method accepts an argument for the person's ID number
    def set_id_number(self, id_number):
        self.__id_number = id_number

    #This method accepts an argument for the person's city of residence
    def set_city(self, city):
        self.__city = city

    #This method returns the employee's name
    def get_name(self):
        return self.__name

    #This method returns the employee's number
    def get_id_number(self):
        return self.__id_number

    #This method returns the employee's city of residence
    def get_city(self):
        return self.__city

    #This method displays a message indicating the employee's name, ID, and city of residence
    def show_employee(self):
        print ('Employee name is', self.__name)
        print ('Employee number is', self.__id_number)
        print ('Employee city is', self.__city)

    #This method displays a message about the volunteer's pay status
    def show_pay(self):
        print()
        print ('This person is an unpaid volunteer.')
        print()

class Hourlyworker(Person):

    #This method calls the superclass's init method and initializes shift and
    #pay rate attributes
    def __init__(self, name, id_number, city, shift, pay_rate):
        Person.__init__(self, name, id_number, city)
        self.__shift = shift
        self.pay_rate = pay_rate

    #This method calculates and displays the hourly and premium pay rate of a 
    #production employee
    def show_pay(self):
        print ('Employee shift is', self.__shift)
        if self.__shift == '1':
            premium_rate = self.__pay_rate
        elif self.__shift == '2':
            premium_rate = (PREMIUM2 * self.__pay_rate) + self.__pay_rate
        else:
            premium_rate = (PREMIUM3 * self.__pay_rate) + self.__pay_rate
        print('Employee hourly premium rate is $', format(premium_rate, '.2f'))

class SalaryWorker(Person):

    #This method calls the superclass's init method and initializes the 
    #pay rate attribute
    def __init__(self, name, id_number, city, pay_rate):
    Person.__init__(self, name, id_number, city)
       self.__pay_rate = pay_rate

    #This method displays the annual salary and bi_weekly gross pay for the
    #salaried employee
    def show_pay(self):
        print('Employee annual salary is $', format(self.__pay_rate, '.2f'))
        bi_weekly_pay = float(self.__pay_rate) / PAY_PERIODS
        print('Employee bi-weekly gross pay is $', format(bi_weekly_pay, '.2f'))
Alex
  • 85
  • 1
  • 6
  • 1
    `self.pay_rate = pay_rate` --> `self.__pay_rate = pay_rate` – jDo Dec 12 '16 at 02:17
  • 1
    In `__init__`, you're missing two underscores: `self.pay_rate = pay_rate`. BTW, double leading underscores is [essentially reserved for Python internal use](http://stackoverflow.com/a/8689983/1270789). – Ken Y-N Dec 12 '16 at 02:18
  • Double leading underscores *with double trailing underscores* are essentially reserved for internal Python use. Double leading underscores, as your link notes, invoke name mangling (a tactic to avoid name conflicts in subclasses). You should feel free to use that feature when appropriate. – kindall Dec 12 '16 at 03:08

0 Answers0