python - Python 的类设计 - 参数错误

标签 python shell python-3.x python-idle

我正在尝试使用以下代码为员工创建 python 代码,但出现错误“Object () 不带参数” 我的目标是创建一个程序来跟踪组织中的员工,允许用户从一个简单的数据库中添加和删除员工,列出他们的员工姓名和号码、工资并更新他们的工资,并将所有信息读取和写入到一个数据库中。文本文件。

到目前为止,这是我的代码:

class Employee:
    """ Employee has name, one staff number, number of years worked, Salary and Holiday"""

    def __init__(self, name, StaffNumber, YearsWorked, Salary, HolidayEntitlement):
        self._name=name
        self._StaffNumber=StaffNumber
        self._YearsWorked=[]
        self._Salary=[]
        self._HolidayEntitlement=[]


    def addEmployee(self) :
        text = int(input("Enter Employee's Name"))
        for i in range (text):
            self._name(input("Enter Employee's Name"))

e = Employee("Jean Green", "13026", "7", "£12,890", "42 days")

print ("Employee: ", e._name)
print ("Employee: ", e._StaffNumber)
print ("Employee: ", e._YearsWorked)
print ("Employee: ", e._Salary)
print ("Employee: ", e._HolidayEntitlement)

我不确定我的代码是否完全错误。

最佳答案

你的缩进是错误的。 __init__方法在您的类下没有正确缩进,因此被视为类外的方法。这使得你的类没有方法并且默认 __init__功能。

将整个行 block 的缩进插入四个空格,您的代码就可以工作了。

关于python - Python 的类设计 - 参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27414726/

相关文章:

linux - Bash shell 函数错误 : command not found

python - 在 Python 中将列表值组合为字符串的好方法是什么

python - 根据另一个列表中的值从元组列表中删除重复值

Linux Shell 脚本 : backup and restore files while maintaining directory location

Bash:在 bash 中用变量回显 echo 命令

python - 通过读取特定字符 ,':' 的左侧来删除行?

python - Visual Studio Code 无法将 python 启动到 wsl 终端

python - Python 2.6 中的随机字符串(可以吗?)

python - 是否可以阻止迭代器到达文件末尾?

python - 了解 PyTorch CNN channel