我通过提供预定义的输入,使用类创建了员工详细信息。我无法将结果存储到dict中。我需要将其作为csv写入。如果您能帮助我,我将不胜感激,因为我是python的新手
以下是我的代码:
class Employee():
def main(self,name,idno,position,salary):
self.name=name
self.idno=idno
self.position=position
self.salary = salary
def input(self):
n=int(raw_input("Enter the number of employees:"))
for i in range(n):
self.name=raw_input("Name:")
self.idno=raw_input("Idno:")
self.position=raw_input("position:")
self.salary=raw_input("salary:")
print("Name:", self.name, "Idno:", self.idno, "position:", self.position,
"salary:", self.salary)
if __name__=='__main__':
result=Employee()
result.input()
首先,我不认为你班上的人会像你想的那样工作。由于您经常重写类变量,因此输入多个雇员是没有意义的,因为类当前只能保存一个雇员的信息。我会考虑将员工保存为字典,并在Employee(s)类中将这些字典保存为列表。在
这说明了我在评论中关于在类的外部使用
for
循环的内容:相关问题 更多 >
编程相关推荐