类对象不会更新空列表

时间:2019-07-07 01:01:53

标签: python loops class object

我正在尝试自行解决此问题。问题要求编写一个有关员工信息的类,然后要求用户输入该信息,最后打印输入的信息。

我想使用两个for循环,一个循环用于获取信息,另一个循环用于打印信息。不幸的是,打印for循环无法正常工作。

class Employee:
    def __init__(self, name, id_num, department, job):
        self.__name = name
        self.__id_num = id_num
        self.__department = department
        self.__job = job

    # setters
    def set_name(self,name):
        self.__name = name
    def set_id(self,id_num):
        self.__id_num = id_num
    def set_department(self,department):
        self.__department = department
    def set_job(self,job):
        self.__job = job

    # getters
    def get_name(self):
        return self.__name
    def get_id(self):
        return self.__id_num
    def get_department(self):
        return self.__department
    def get_job(self):
        return self.__job

def main():

    employee_list = []

    for i in range(2):
        name = input('What is the name of the employee? ')
        id_num = float(input('What is the ID number of the employee? '))
        department = input('What is the department where the employee works? ')
        job = input('What is the job title of the empoyee? ')

        personnel = Employee(name,id_num,department,job)

        employee_list.append(personnel)
    return employee_list


    for item in employee_list:
        print(item.get_name())
        print(item.get_id())
        print(item.get_department())
        print(item.get_job())
        print()


main()

1 个答案:

答案 0 :(得分:2)

您需要在main()函数中删除以下行:

return employee_list

这导致您的主电源停止运行,而未到达打印循环。

相关问题