Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
缺少1个必需的位置参数,且参数';没有值;自我';基于类的python程序中的未绑定方法错误_Python_Oop - Fatal编程技术网

缺少1个必需的位置参数,且参数';没有值;自我';基于类的python程序中的未绑定方法错误

缺少1个必需的位置参数,且参数';没有值;自我';基于类的python程序中的未绑定方法错误,python,oop,Python,Oop,为什么我会出现这些错误: 控制台:“TypeError:printTodos()缺少1个必需的位置参数:“self” VS代码中最后两行“TodoList”下的扭曲下划线,错误为“未绑定方法callpylint中的参数'self'没有值(参数没有值)” 谢谢你的建议 从此改变 while (True): TodoList.add_todo() TodoList.printTodos() 对此 while (True): todo_list.add_todo()

为什么我会出现这些错误: 控制台:“TypeError:printTodos()缺少1个必需的位置参数:“self”

VS代码中最后两行“TodoList”下的扭曲下划线,错误为“未绑定方法callpylint中的参数'self'没有值(参数没有值)”

谢谢你的建议

从此改变

while (True):
    TodoList.add_todo()
    TodoList.printTodos()
对此

while (True):
    todo_list.add_todo()
    todo_list.printTodos()

init参数中需要有缩进宽度

class TodoList:
    def __init__(self, todos, indent_width):
        self.todos = todos
        self.count = 1
        self.indent_width = "  "

    def add_todo(self):
        response = input("Please input a todo: \n")
        self.todos.append(self.indent_width + response)

    def printTodos(self):
        for todo in self.todos:
            print(self.indent_width + str(self.count) + "." + todo)
            self.count += 1

todo_list = TodoList(["Have fun", "Chill"])

while (True):
    TodoList.add_todo()
    TodoList.printTodos()

因为您试图在类上调用它,而不是在实例上调用它。当在类上访问时,它只是一个常规函数,在这种情况下,它需要您指定的参数。您可能打算使用
todo_list
,实例可能也适合阅读上面的Python文档。
class TodoList:
    def __init__(self, todos, indent_width):
        self.todos = todos
        self.count = 1
        self.indent_width = "  "

    def add_todo(self):
        response = input("Please input a todo: \n")
        self.todos.append(self.indent_width + response)

    def printTodos(self):
        for todo in self.todos:
            print(self.indent_width + str(self.count) + "." + todo)
            self.count += 1

todo_list = TodoList(["Have fun", "Chill"])

while (True):
    TodoList.add_todo()
    TodoList.printTodos()