Python与类的操作
我正在使用Python 3编写类,但遇到了一些困难。我这里有两个程序(一个程序导入了另一个程序)。
我的想法是创建一个员工列表,并且有三种员工类型:按小时计薪的员工、固定薪资的员工和志愿者。
我发现我在每个类中的show_pay方法有问题。我还知道在我的固定薪资类中,我试图用一个字符串去除以一个整数,但我写的代码让我不太确定该怎么解决这个问题。
我的按小时计薪的类似乎没有打印到列表中。
谢谢你们的帮助。我对此感到很困惑,正在努力完成这个项目。
第一个项目(员工)
#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.')
这是主程序
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()
1 个回答
1
你输入的工资是以字符串的形式出现的。需要把它转换一下。
ann_salary = int(input('Enter employee annual salary: '))