Reputation: 863
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.
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.
Thank you in advance. I'm really confused with this and I'm trying to get through this project.
First Project (Employee)
#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.')
This is the main program
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 > 0:
ID_valid = True
else:
print("\nID must be > 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()
Upvotes: 0
Views: 2546
Reputation: 798456
You're inputting the salary as a string. Convert it.
ann_salary = int(input('Enter employee annual salary: '))
Upvotes: 1