使用python编写类打印每个员工的数据

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

我正在使用 python 编写一个 Employee 类,以便我能够打印输入中每个员工的数据。

  • 我必须编写一个名为employee.py的文件,其中有一个名为Employee的类并存储以下属性:姓名、身份证号、部门和职位

  • 编写一个名为employee_test.py的程序,创建三个员工对象来保存数据

  • 例如:Susan Meyers,47889,会计,副总裁

我尝试了下面的代码,但遇到了模块错误,我检查以确保它也已安装。我期待看到打印出的名字和他们的信息。

class Employee
def __init__(self, name, id_num, dep, title):
    self.__name = name
    self.__id = id_num
    self.__dep = dep
    self.__title = title

def set_id(self, id_num):
    self.__id = id_num

def set_dep(self, dep):
    self.__dep  = dep

def set_title(self, title):
    self.__title = title
def __str__(self):
    result = f"{self__name}/t{self.__id/t{self.__dep}/t{self.__title}"
    return result

from employee import Employee 

def main():
    Susan = Employee("Susan Meyers", "47899", "Accounting", "Vice President")
    Mark = Employee("Mark Jones", "39119", "IT", "Programmer")
    Joy = Employee("Joy Rogers", "81774", "Manufacturing", "Engineer")

    print(Susan)
    print(Mark)
    print(Joy)

main()
   

Traceback (most recent call last):
  File "C:/Users/19013/employee_test.py", line 1, in <module>
    from employee import Employee
ModuleNotFoundError: No module named 'employee'
python class
1个回答
0
投票

从您的回溯中,您的文件名是

employee_test.py
,但您正在尝试从
employee
导入。您的导入声明应该是:

from employee_test import Employee
© www.soinside.com 2019 - 2024. All rights reserved.