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