循环不会在python程序中执行

时间:2017-03-10 05:01:11

标签: python

class Employee:

    def set_name(self, name):
        self.name = name

    def set_IDnumber(self, IDnumber):
        self.IDnumber = IDnumber

    def set_department(self, deparment):
        self.deparment = deparment

    def set_jobTitle(self, jobTitle):
        self.jobTitle = jobTitle

    def get_name(self):
        # Returns name
        return self.name

    def get_IDnumber(self):
        # Returns IDnumber
        return self.IDnumber

    def get_department(self):
        # Returns department
        return self.department

    def get_jobTitle(self):
        # Returns job title
        return self.jobTitle

def make_list():
    employee_list = []
    print("Enter data for 3 employees")
    for i in range(1, 4):
        print('Employee ' + str(i))
        name = input('Enter employee name: ')
        idNumber = int(input('Enter employee ID number: '))
        department = input('Enter employee department: ')
        jobTitle = input('Enter job title: ')
        employee = Employee()
        employee.set_name(name)
        employee.set_IDnumber(idNumber)
        employee.set_department(department)
        employee.set_jobTitle(jobTitle)
        employee_list.append(employee)
        print(i)
        return employee_list


def main():
    make_list()

main()

程序运行正常。然而,我遇到的问题是程序在循环的一次迭代后停止,因此我只能输入一个雇员的信息。任何人都可以告诉我为什么它只对3名员工进行一次而不是3次迭代?万分感谢。

1 个答案:

答案 0 :(得分:0)

问题在于return语句。 return语句在循环内部,它应该在循环之外。

Make_list函数应替换为以下代码。

def make_list():
    employee_list = []
    print("Enter data for 3 employees")
    for i in range(1, 4):
        print('Employee ' + str(i))
        name = input('Enter employee name: ')
        idNumber = int(input('Enter employee ID number: '))
        department = input('Enter employee department: ')
        jobTitle = input('Enter job title: ')
        employee = Employee()
        employee.set_name(name)
        employee.set_IDnumber(idNumber)
        employee.set_department(department)
        employee.set_jobTitle(jobTitle)
        employee_list.append(employee)
        print(i)
    return employee_list