[英]create a list from class user input
emp_object = []
for i in range(5):
emp_ object.append(ClassName())
I'm trying to do this exercise and this is what I got:我正在尝试做这个练习,这就是我得到的:
class employee:
def __init__(self, n = None, a = None, i = None, s = None):
self.name = n
self.age = a
self.id = i
self.salary = s
def setData(self):
self.n = input("Enter name: ")
self.a = int(input("Enter age: "))
self.i = int(input("Enter id: "))
self.s = int(input("Enter salary: "))
self.getData()
def getData(self):
print("Name:", self.name, self.age, self.id, self.salary)
e1 = employee()
e1.setData()
e2 = employee()
e2.setData()
e3 = employee()
e3.setData()
e4 = employee()
e4.setData()
e5 = employee()
e5.setData()
emp_object = []
for i in range(5):
emp_object.append(employee())
print(emp_object)
It prints the employee details as "None" and I need help to create a list它将员工详细信息打印为“无”,我需要帮助来创建列表
Expected Output:预计 Output:
Name id Age Salary
AAA 20 1 2000
BBB 22 2 2500
CCC 20 3 1500
DDD 22 4 3500
EEE 22 5 4000
Change the instance variable self.n ( in the setData method) to self.name to match the declaration your class init method...and do the same for the self.a, self.i... variables.将实例变量 self.n(在 setData 方法中)更改为 self.name 以匹配您的 class init 方法的声明...并对 self.a、self.i... 变量执行相同的操作。
I beleive the problem is that you are not setting the parameters to the ones you want in the setData function.我相信问题在于您没有将参数设置为您在 setData function 中想要的参数。
You need to do this:你需要这样做:
class employee:
def __init__(self, n = None, a = None, i = None, s = None):
self.name = n
self.age = a
self.id = i
self.salary = s
def setData(self):
self.name = input("Enter name: ")
self.age = int(input("Enter age: "))
self.id = int(input("Enter id: "))
self.salary = int(input("Enter salary: "))
self.getData()
def getData(self):
print("Name:", self.name, self.age, self.id, self.salary)
The __init__
and setData
are two separate functions. __init__
和setData
是两个独立的函数。
First you want to separate some responsabilities for a better reading.首先,您想分离一些职责以便更好地阅读。 We will divide the problem in two parts:我们将问题分为两部分:
Create a class who contains only employee data (we can use dataclasses but, I assume you're a beginner, so I'll keep simple)创建一个仅包含员工数据的 class(我们可以使用数据类,但我假设您是初学者,所以我会保持简单)
class Employee:
def __init__(self, uid=None, name=None, age=None, salary=None):
self.name = name
self.age = age
self.id = uid
self.salary = salary
To display the employee's data in console, we can use __str__
function. It is used when you class need to be converted into a str
(in print
for isntance).要在控制台显示员工的数据,我们可以使用__str__
function 。当您需要将 class 转换为str
时使用它(in print
for isntance)。 We then add an other method in charge to set employee's data.然后我们添加另一个负责设置员工数据的方法。 Our Employee
class become:我们的Employee
class 变成:
class Employee:
def __init__(self, uid=None, name=None, age=None, salary=None):
self.name = name
self.age = age
self.id = uid
self.salary = salary
def __str__(self):
return f"Name: {self.name}, {self.age}, {self.id}, {self.salary}"
def set_data(self):
self.name = input("Enter name: ")
self.age = int(input("Enter age: "))
self.id = int(input("Enter id: "))
self.salary = int(input("Enter salary: "))
Our class is complete.我们的class就完成了。 Now we will write the algorithm in charge to create 5 employees.现在我们将编写负责创建 5 名员工的算法。 So under the Employee
class:所以在Employee
class 下:
if __name__ == '__main__':
# Empty list containing our employees
employees = []
# We loop 5 times.
for i in range(5):
# We create an employee
employee = Employee()
# We set the data
employee.set_data()
# We append our brand-new employee into the list
employees.append(employee)
# Now we display our data :
for employee in employees:
# We just need to print the object thanks to __str__ method
print(employee)
Tell me if I answered correctly to your problem !告诉我我是否正确回答了您的问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.