1

这是一个程序,它应该提出一系列问题,保存信息,然后将信息显示给您。每当我运行该程序时,它都会成功询问所有信息,但在尝试显示其中的某个部分时会出错。

错误是:

Traceback (most recent call last):
 , line 27, in <module>
    main()
 , line 24, in main
    print ("Shift: ", worker.get_shift_number())
AttributeError: 'Worker' object has no attribute 'get_shift_number'

代码是:

# employee.py
class Employee(object):
    def __init__(self, name, id_number):
        self.__id_number = id_number
        self.__name = name
    def set_name(self, name):
        self.__name = name
    def set_id_number(self, id_number):
        self.__id_number = id_number
    def get_name(self):
        return self.__name
    def get_id_number(self):
        return self.__id_number

class Worker(Employee):
    def __init__(self, name, id_number, shift_number, pay_rate):
        #call superclass __init__ method
        Employee.__init__(self, name, id_number)
        #initialize shift_number and pay_rate attributes
        self.__shift_number = shift_number
        self.__pay_rate = pay_rate
        #mutator functions for shift_number and pay_rate
        def set_shift_number(self, shift_number):
            self.__shift_number = shift_number
        def set_pay_rate(self, pay_rate):
            self.__pay_rate = pay_rate
        #accessor functions for shift_number and pay_rate
        def get_shift_number(self):
            return self.__shift_number
        def get_pay_rate(self):
            return self.__pay_rate

然后我们有这个。

#workerprogram.py
import employee

def main():
    #variables
    worker_name= " "
    worker_id = " "
    worker_shift = 0
    worker_pay = 0.0

    #get data attributes
    worker_name = input("Enter the worker name: ")
    worker_id = input("Enter the ID number: ")
    worker_shift = float(input("Enter the shift number: "))
    worker_pay = float(input("Enter the hourly pay rate: "))

    #create an instance of Worker
    worker = employee.Worker(worker_name, worker_id, worker_shift, worker_pay)

    #display information
    print ("Production worker information: ")
    print ("Name: ", worker.get_name())
    print ("ID number: ", worker.get_id_number())
    print ("Shift: ", worker.get_shift_number())
    print ("Hourly Pay Rate: $", worker.get_pay_rate())

main()

如前所述,缩进是错误的,而且我还看到我的 shift_number 初始化在错误的位置。;(

谢谢!

4

1 回答 1

0

由于 Rob 删除了他的正确答案:你的问题是你的缩进是错误的。

但是,这不是代码的唯一问题。主要的是它过于复杂,因为您对所有内容都有访问器/修改器,这不是必需的,并且只会浪费您输入它们和使用它们的计算机的时间。

这是您的代码的外观:

# employee.py
class Employee(object):

    def __init__(self, name, id_number):
        self.id_number = id_number
        self.name = name

class Worker(Employee):

    def __init__(self, name, id_number, shift_number, pay_rate):
        #call superclass __init__ method
        Employee.__init__(self, name, id_number)
        #initialize shift_number and pay_rate attributes
        self.shift_number = shift_number
        self.pay_rate = pay_rate

#workerprogram.py
import employee

def main():
    #variables
    worker_name= " "
    worker_id = " "
    worker_shift = 0
    worker_pay = 0.0

    #get data attributes
    worker_name = input("Enter the worker name: ")
    worker_id = input("Enter the ID number: ")
    worker_shift = float(input("Enter the shift number: "))
    worker_pay = float(input("Enter the hourly pay rate: "))

    #create an instance of Worker
    worker = employee.Worker(worker_name, worker_id, worker_shift, worker_pay)

    #display information
    print ("Production worker information: ")
    print ("Name: ", worker.name)
    print ("ID number: ", worker.id_number)
    print ("Shift: ", worker.shift_number)
    print ("Hourly Pay Rate: $", worker.pay_rate)

main()

尽管诚然,我认为您实际上既不希望班次人数也不希望工资率浮动,但这可以等到另一个问题。

于 2013-11-11T22:03:29.980 回答