我正在从名为 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
`
我认为你只需要将你创建的每个员工附加到一个列表中,然后在你附加完所有 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)