Note that there are some explanatory texts on larger screens.

plurals
  1. POPython working with classes
    primarykey
    data
    text
    <p>I am working with classes in Python 3 and I am having a hard time with them. I have two programs here (One is being imported into the other) The idea is that you are making a list of employees and you have three classes of employee Hourly, Salary, and Volunteer.</p> <p>I seem to be having an issue with my show_pay in each class. I also know that in my Salary class I am attempting to divide a string by an integer but the way I have the code written I'm not quite sure how to get around it. My Hourly class doesn't seem to print to the list.</p> <p>Thank you in advance. I'm really confused with this and I'm trying to get through this project.</p> <p>First Project (Employee)</p> <pre><code> #set global constant SHIFT_2 = 0.05 SHIFT_3 = 0.10 #person class class Person: #initialize name, ID number, city def __init__(self, name, ID, city): self.__ID = ID self.__name = name self.__city = city #display employee name def show_person(self): print('Name:', self.__name) print('ID:', self.__ID) print('City:', self.__city) #display salary def show_pay(self): print('I make lots of money') #return formatting def __str__(self): name_string = '(My name is ' + self.__name +')' return name_string # Hourly employee class class Hourly(Person): #initialize method calls superclass def __init__(self, name, ID, city, base_pay, shift): Person.__init__(self, name, ID, city) self.__base_pay = base_pay self.__shift = shift #show_pay overrides the superclass and displays hourly pay rates def show_pay(self): if self.__shift == 1: print('My salary is ', self.__base_pay) elif self.__shift == 2: print('My salary is ', (self.__base_pay * SHIFT_2) + self.__base_pay) elif self.__shift == 3: print('My salary is ', (self.__base_pay * SHIFT_3) + self.__base_pay) #salary employee class class Salary(Person): #intialize method calls superclass def __init__(self, name, ID, city, ann_salary): Person.__init__(self, name, ID, city) self.__salary = ann_salary #show pay overrides superclass and displays salary pay rates def show_pay(self): print('I make ', self.__salary) print('which is ', self.__salary // 26, 'every two weeks.') #volunteer employee class class Volunteer(Person): def __init__(self, name, ID, city): Person.__init__(self, name, ID, city) def show_pay(self): print('I am a volunteer so I am not paid.') </code></pre> <p>This is the main program</p> <pre><code> import employee def main(): #create list employees = make_list() #display list print('Here are the employees.') print('-----------------------') display_list(employees) def make_list(): #create list employee_list = [] #get number of hourly employees number_of_hourly = int(input('\nHow many hourly will be entered? ')) for hourly in range(number_of_hourly): #get input name, ID, city = get_input() base_pay = input('Enter employee base pay: ') shift = input('Enter employee shift 1,2, or 3: ') #create object Hourly = employee.Hourly(name, ID, city, base_pay, shift) #add object to list employee_list.append(Hourly) #get number of salary employees number_of_salary = int(input('\nHow many salary will be entered? ')) for salary in range(number_of_salary): #get input name, ID, city = get_input() ann_salary = input('Enter employee annual salary: ') #create object salary = employee.Salary(name, ID, city, ann_salary) #add object to list employee_list.append(salary) #get volunteers number_of_volunteers = int(input('\nHow many other volunteers will be entered? ')) for volunteers in range(number_of_volunteers): #get info name, ID, city = get_input() #create object volunteer = employee.Person(name, ID, city) #add object to list employee_list.append(volunteer) #invalid object employee_list.append('\nThis is invalid') #return employee_list return employee_list def get_input(): #input name name = input("Employee's name: ") #validate while name == '': print('\n Name is required. Try again.') name = input("Employee's name: ") ID_valid = False ID = input("Employee's ID: ") while ID_valid == False: try: ID = float(ID) if ID &gt; 0: ID_valid = True else: print("\nID must be &gt; 0. Try again.") ID = input("Employee's age: ") except ValueError: print("\nID must be numeric. Try again.") ID = input("Employee's ID: ") #get city city = input("Enter employee's city of residence: ") #return values return name, ID, city def display_list(human_list): #create for loop for isinstance for human in human_list: #create isinstance if isinstance(human, employee.Person): print(human) human.show_person() human.show_pay() print else: print('Invalid employee object') #call main function main() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload