在基于类的python程序中的未绑定方法错误中缺少1个必需的位置参数并且没有参数“self”的值

问题描述

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

在最后 2 行“TodoList”下的 VS 代码中用波浪线下划线,并显示错误“未绑定方法 callpylint(no-value-for-parameter) 中参数‘self’没有值”

class TodoList:
    def __init__(self,todos):
        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()

感谢您的建议!

解决方法

从此改变

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

为此

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

您需要在 init 参数中包含 indent_width

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()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...