Python在函数

时间:2015-07-16 22:01:42

标签: python function class

我之前从未使用过课程,而且我正在尝试大致了解它们如何使用我在下面的代码示例。我有问题引用我为一个类定义的名称之一。我只是希望程序在输入选项2时打印出列表中存储的员工姓名和工资列表,但它给出了以下错误:

追踪(最近一次通话):   文件" C:\ Scott Glenn \ Misc \ classes.py",第31行,in     员工[I] .displayEmployee AttributeError:' str'对象没有属性' displayEmployee'

class Employee:
    'Common base class for all employees'
    empCount = 0

    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
        Employee.empCount += 1

    def displayCount(self):
        print "Total Employee %d" % Employee.empCount

    def displayEmployee(self):
        print "Name : ", self.name,  ", Salary: ", self.salary

def AddNewEmployee():
    NewEmployee = raw_input("What is the Employees name: ")
    employees.append(str(NewEmployee))
    NewEmployeeSalary = raw_input("What is the Employees salary: ")
    NewEmployee = Employee(NewEmployee, NewEmployeeSalary)
    return employees
#=============================================================================
employees=[]
while(1):
    print'Welcome to the Employee Database!'
    option = raw_input('Please select 1 to add new employee or 2 to      display all current employees: ')
    if option=='1':
        employees.append(AddNewEmployee())
    if option=='2':
        for i in range(0,len(employees)):
            employees[i].displayEmployee

2 个答案:

答案 0 :(得分:4)

AddNewEmployee功能错误。当您想要返回自定义类型list的单个对象时,它会返回单string的{​​{1}}。

应该更像这样:

Employee

此外,您尝试访问def AddNewEmployee(): #string variable to hold name NewEmployeeName = raw_input("What is the Employees name: ") #why make a list? you are appending the result of this function to that list #employees.append(str(NewEmployee)) #plus this is adding the employee before he's even been created NewEmployeeSalary = raw_input("What is the Employees salary: ") #construct using name string and salary string NewEmployee = Employee(NewEmployeeName, NewEmployeeSalary) return NewEmployee #return Employee object (to be appended later) 作为类的字段,而不是方法。字段不具有括号和方法(因此它们可以接受参数,但在这种情况下,括号为空,因为没有参数传递)。

最后,请注意displayEmployee()会返回raw_input,因此如果您希望自己string成为float,则应该转为NewEmployeeSalary。 (现在它是string。)

答案 1 :(得分:0)

我已在下面更新了您的代码。我看到你遇到的主要问题是你使用“员工”作为全球性并且两次追加它。我将它移出AddNewEmployee()函数并返回新员工,然后将其附加到'employees'

你也没有打电话给'.displayEmployees' 注意我添加到最后的括号。

我希望这有帮助!

class Employee(object):
    'Common base class for all employees'
    empCount = 0

    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
        Employee.empCount += 1

    def displayCount(self):
        print "Total Employee %d" % Employee.empCount

    def displayEmployee(self):
        print "Name : ", self.name, ", Salary: ", self.salary


def AddNewEmployee():
    NewEmployee = raw_input("What is the Employees name: ")
    NewEmployeeSalary = raw_input("What is the Employees salary: ")
    NewEmployee = Employee(NewEmployee, NewEmployeeSalary)
    return NewEmployee


# =============================================================================

if __name__ == "__main__":

    employees = []
    while True:
        print'Welcome to the Employee Database!'
        option = raw_input(
            'Please select 1 to add new employee or 2 to display all current employees: ')
        if option == '1':
            employees.append(AddNewEmployee())
        if option == '2':
            for i in range(0, len(employees)):
                employees[i].displayEmployee()