问题描述
为什么我会收到这些错误: 控制台:“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()