我对以下程序的输出感到困惑。由于某种原因,它抛出指定的错误消息'那不是一个有效的员工类型!'但我不确定为什么我输入的所有信息都是有效的。
下面的10.py程序
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()
这是下面的worker.py代码
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'))
这是我运行程序时的输入和结果。如果您注意到,在输出结尾处显示“那不是有效的员工类型!”但我不明白为什么这么说。
What is the employee name? al
What is the employee's number? 1
What is the employee's city? h
Create 3 hourly worker objects:
What is the employee number 2's name? bl
What is the employee's number? 2
What is the employee's city? howell
What is the employee's shift? 1
What is the employee's hourly pay rate? 10
What is the employee number 3's name? cl
What is the employee's number? 3
What is the employee's city? howell
What is the employee's shift? 2
What is the employee's hourly pay rate? 10
What is the employee number 4's name? dl
What is the employee's number? 4
What is the employee's city? howell
What is the employee's shift? 3
What is the employee's hourly pay rate? 10
Create a volunteer worker object
What is the salaried employee's name? el
What is the employee's number? 5
WHat is the employee's city? howell
What is the employee's annual salary? 10000
Here are the workers, their ID numbers, city of residence, shift, and pay.
------------------------------------------------------------------------------
Employee name is al
Employee number is 1
Employee city is h
This person is an unpaid volunteer.
Employee name is bl
Employee number is 2
Employee city is howell
Employee shift is 1
Employee hourly premium rate is $ 10.00
Employee name is cl
Employee number is 3
Employee city is howell
Employee shift is 2
Employee hourly premium rate is $ 10.50
Employee name is dl
Employee number is 4
Employee city is howell
Employee shift is 3
Employee hourly premium rate is $ 11.00
Employee name is el
Employee number is 5
Employee city is howell
Employee annual salary is $ 10000.00
Employee bi-weekly gross pay is $ 384.62
That is not a valid employee type!
答案 0 :(得分:2)
你故意执行:
worker_list.append('\nThis is an invalid object')
返回worker_list
中的make_list
之前。看起来您希望它执行此操作,毕竟str
不是worker.Person
的实例,因此else
中的display_list
案例应该让您知道list
{1}}条目无效。
答案 1 :(得分:0)
此行正在向worker_list
添加无效的工作对象(只是一个字符串)worker_list.append('\ n这是一个无效的对象')
如果你发表评论,那么isinstance测试不会命中它并失败。