簡體   English   中英

不了解python程序的輸出

[英]Don't understand output from program python

我對下面程序的輸出感到困惑。 由於某種原因,它會拋出指定的錯誤消息“這不是有效的員工類型!” 但是我不知道為什么,因為我輸入的所有信息都是有效的。

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!

您有意執行:

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

返回worker_list中的make_list 似乎您希望這樣做,畢竟str不是worker.Person的實例,因此display_listelse情況應該讓您知道該list無效的條目。

該行向您的worker_list添加了一個無效的worker對象(只是一個字符串)

將無效的對象追加到列表

worker_list.append('\\ n這是無效的對象')

如果您將其注釋掉,則isinstance測試不會命中並失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM