Python 从类中获取多个用户输入

问题描述 投票:0回答:1

我正在从名为 Employee 的类中进行多个用户输入。我的任务是需要输入至少6个员工,一次性全部打印出来。应该怎么做才能解决这个问题?

`class Employee:

    def __init__ (self, number, first, middle, last, address, rate, salary):
        self.number = number
        self.first = first
        self.middle = middle
        self.last = last
        self.address = address
        self.rate = rate
        self.salary = rate*28


    def fullname(self):
        return '{} {} {} {} {} {} {} {}'.format("Empoloyee Number: " + self.number + "\n",
                                                "First Name: " + self.first +"\n",
                                                "Middle Name: " + self.middle + "\n",
                                                "Last Name: "+ self.last + "\n",
                                                "Address: " + self.address + "\n Rate: ",
                                                self.rate,
                                                "\n Salary: ", self.salary)


i=1
while i <7:
    number = input("Enter Employee Number: ")
    first = input("Enter First Name: ")
    middle = input("Enter Middle Name: ")
    last = input("Enter Last Name: ")
    address = input("Enter Address: ")
    rate = float(input("Enter Rate: "))

    salary = rate*28

   emp = (Employee(number, first, middle, last, address, rate, salary))
   print (emp.fullname())
   i+=1

`
python python-3.x list python-2.7 pycharm
1个回答
0
投票

我认为你只需要将你创建的每个员工附加到一个列表中,然后在你附加完所有 6 个之后打印列表。

def __init__ (self, number, first, middle, last, address, rate, salary):
    self.number = number
    self.first = first
    self.middle = middle
    self.last = last
    self.address = address
    self.rate = rate
    self.salary = rate*28


def fullname(self):
    return '{} {} {} {} {} {} {} {}'.format("Empoloyee Number: " + self.number + "\n",
                                            "First Name: " + self.first +"\n",
                                            "Middle Name: " + self.middle + "\n",
                                            "Last Name: "+ self.last + "\n",
                                            "Address: " + self.address + "\n Rate: ",
                                            self.rate,
                                            "\n Salary: ", self.salary)


employees = []

i=1
while i<=6:
    number = input("Enter Employee Number: ")
    first = input("Enter First Name: ")
    middle = input("Enter Middle Name: ")
    last = input("Enter Last Name: ")
    address = input("Enter Address: ")
    rate = float(input("Enter Rate: "))

    salary = rate*28

    emp = Employee(number, first, middle, last, address, rate, salary))
    employees.append(emp)

print(employees)
© www.soinside.com 2019 - 2024. All rights reserved.