0
Follow
0
View

python learning, questionnaire survey error

ddl301 注册会员
2023-02-25 20:08

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.

chengcyx 注册会员
2023-02-25 20:08

The answer part refers to GPT, GPT_Pro better solve the problem
The possible question here is what should be written in parentheses after class in line 5? You should fill in a meaningful class name for this test class, such as TestAnonymoussurvey.

Line 9 question = should be followed by a string indicating the question to be investigated, such as' What is your favorite sport? '

Line 11 my_survey-response() should be followed by a string to indicate the investigator's response, such as' football '

Line 13 should be followed by self.assertIn() with a variable indicating the investigator's response, such as' football '.

So the code should look like this:

import unittest
from survey import Anonymoussurvey

class TestAnonymoussurvey(unittest.TestCase):

    def test_store_single_response(self):
        question = '你最喜欢的运动是什么?'
        my_survey = Anonymoussurvey(question)
        my_survey.store_response('足球')

        self.assertIn('足球', my_survey.responses)

unittest.main()

If the answer is helpful, please accept it.