the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > your code without a colon(:) led to a syntax error, cause the program can't perform well. You can try modifying the code in the for loop with a colon and try again, as follows:
class AnonymousSurvey():
def __int__(self,question):
'''存储一个问题,并为存储答案做准备'''
self.question = question
self.responses=[]
def show_question(self):
'''显示调查问卷'''
print(self.question)
def response(self,new_response):
'''存储单份调查问卷'''
self.responses.append(new_response)
def show_results(self):
'''显示收集到的所有答卷'''
print("Survey result:")
for response in self.responses: # 加上冒号
print(f"- {response}")
Before testing, you also need to change the __int__ method in the AnonymousSurvey class to __init__, otherwise the following error will occur when instantiating the AnonymousSurvey class: TypeError: 'AnonymousSurvey' object is not callable. Therefore, the complete code should look like this:
import unittest
from survey import AnonymousSurvey
class TestAnonymousSurvey(unittest.TestCase):
'''针对AnonymousSurvey类的测试'''
def test_store_single_response(self):
question = "What language did you learn to speak?"
my_survey = AnonymousSurvey(question)
my_survey.store_response('English')
self.assertIn('English',my_survey.responses)
unittest.main()
class AnonymousSurvey():
def __init__(self,question): # 将 __int__ 改为 __init__
'''存储一个问题,并为存储答案做准备'''
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
In this case, the program should run properly and output the test results.