引起TypeError: ClassName() takes no arguments错误的情况有很多种,接下来我将介绍其中一种情况,为未对类中函数缩进而引起的错误。
我们创建如下代码
survey.py
class AnonymousSurvey:
"""收集匿名调查问卷的答案。"""
def __init__(self, question):
"""存储一个问题,并为存储答案做准备。"""
self.question = question
self.responses = []
def show_question(self):
"""显示调查问卷。"""
print(self.question)
def store_response(self, new_response):
"""存储单份调查答卷。"""
self.responses.append(new_response)
def show_results(self):
"""显示收集到的所有答卷。"""
print("Survey results:")
for response in self.responses:
print(f"- {response}")
创建以上代码,运行,程序并不会有任何错误和问题提示。如果进一步运行下面的代码,错误就会出现。
tes