我正在使用Python 3中的类,我很难用它们。我这里有两个程序(一个是导入另一个) 这个想法是你正在制作一份员工清单,你有三类员工每小时,薪水和志愿者。
我似乎在每个课程中都遇到了show_pay的问题。我也知道在我的Salary类中,我试图将一个字符串除以整数,但是我编写代码的方式我不太清楚如何绕过它。 我的每小时课程似乎没有打印到列表中。
提前谢谢你。我真的很困惑,我正试图通过这个项目。
第一个项目(员工)
#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()
答案 0 :(得分:1)
您输入的薪水是一个字符串。转换它。
ann_salary = int(input('Enter employee annual salary: '))