2016-12-12 13 views
-1

以下のプログラムの出力と混同しています。なんらかの理由で、指定されたエラーメッセージ 'これは有効な従業員タイプではありません!'私が入力したすべての情報が有効だった理由は分かりません。プログラムの出力を理解しないpython

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() 

以下

10.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')) 

以下worker.pyコードが入力され、iがプログラムを実行するときに生じるれます。出力の最後に「それは有効な従業員タイプではありません!」と表示されます。私はなぜそれが言っているのか分かりません。

あなたが意図的に行っ
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

これを[mcve]に減らしてください。 – TigerhawkT3

+0

コードで 'That is not valid employee type! 'を検索すると、その行がどこにあり、どのように到達したのかを知ることができます(' if'述部はfalseです) –

答えて

2

worker_list.append('\nThis is an invalid object') 

make_listworker_listを返す前に。結局のところ、strはのインスタンスではないので、display_listelseのケースでは、listに無効なエントリがあることを知らせることになっています。

0

このラインは(「\ nこれは無効なオブジェクトである」)あなたのworker_listに

worker_list.appendをリストする

アペンド無効なオブジェクトを無効ワーカーオブジェクト(単なる文字列)を追加していますあなたがそれをコメントアウトすると、isinstanceテストはそれに当たって失敗するでしょう。

関連する問題